spi_ppc4xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * SPI_PPC4XX SPI controller driver.
  3. *
  4. * Copyright (C) 2007 Gary Jennejohn <garyj@denx.de>
  5. * Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering
  6. * Copyright 2009 Harris Corporation, Steven A. Falco <sfalco@harris.com>
  7. *
  8. * Based in part on drivers/spi/spi_s3c24xx.c
  9. *
  10. * Copyright (c) 2006 Ben Dooks
  11. * Copyright (c) 2006 Simtec Electronics
  12. * Ben Dooks <ben@simtec.co.uk>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License version 2 as published
  16. * by the Free Software Foundation.
  17. */
  18. /*
  19. * The PPC4xx SPI controller has no FIFO so each sent/received byte will
  20. * generate an interrupt to the CPU. This can cause high CPU utilization.
  21. * This driver allows platforms to reduce the interrupt load on the CPU
  22. * during SPI transfers by setting max_speed_hz via the device tree.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <linux/errno.h>
  29. #include <linux/wait.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/of_spi.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/delay.h>
  35. #include <linux/gpio.h>
  36. #include <linux/spi/spi.h>
  37. #include <linux/spi/spi_bitbang.h>
  38. #include <asm/io.h>
  39. #include <asm/dcr.h>
  40. #include <asm/dcr-regs.h>
  41. /* bits in mode register - bit 0 is MSb */
  42. /*
  43. * SPI_PPC4XX_MODE_SCP = 0 means "data latched on trailing edge of clock"
  44. * SPI_PPC4XX_MODE_SCP = 1 means "data latched on leading edge of clock"
  45. * Note: This is the inverse of CPHA.
  46. */
  47. #define SPI_PPC4XX_MODE_SCP (0x80 >> 3)
  48. /* SPI_PPC4XX_MODE_SPE = 1 means "port enabled" */
  49. #define SPI_PPC4XX_MODE_SPE (0x80 >> 4)
  50. /*
  51. * SPI_PPC4XX_MODE_RD = 0 means "MSB first" - this is the normal mode
  52. * SPI_PPC4XX_MODE_RD = 1 means "LSB first" - this is bit-reversed mode
  53. * Note: This is identical to SPI_LSB_FIRST.
  54. */
  55. #define SPI_PPC4XX_MODE_RD (0x80 >> 5)
  56. /*
  57. * SPI_PPC4XX_MODE_CI = 0 means "clock idles low"
  58. * SPI_PPC4XX_MODE_CI = 1 means "clock idles high"
  59. * Note: This is identical to CPOL.
  60. */
  61. #define SPI_PPC4XX_MODE_CI (0x80 >> 6)
  62. /*
  63. * SPI_PPC4XX_MODE_IL = 0 means "loopback disable"
  64. * SPI_PPC4XX_MODE_IL = 1 means "loopback enable"
  65. */
  66. #define SPI_PPC4XX_MODE_IL (0x80 >> 7)
  67. /* bits in control register */
  68. /* starts a transfer when set */
  69. #define SPI_PPC4XX_CR_STR (0x80 >> 7)
  70. /* bits in status register */
  71. /* port is busy with a transfer */
  72. #define SPI_PPC4XX_SR_BSY (0x80 >> 6)
  73. /* RxD ready */
  74. #define SPI_PPC4XX_SR_RBR (0x80 >> 7)
  75. /* clock settings (SCP and CI) for various SPI modes */
  76. #define SPI_CLK_MODE0 (SPI_PPC4XX_MODE_SCP | 0)
  77. #define SPI_CLK_MODE1 (0 | 0)
  78. #define SPI_CLK_MODE2 (SPI_PPC4XX_MODE_SCP | SPI_PPC4XX_MODE_CI)
  79. #define SPI_CLK_MODE3 (0 | SPI_PPC4XX_MODE_CI)
  80. #define DRIVER_NAME "spi_ppc4xx_of"
  81. struct spi_ppc4xx_regs {
  82. u8 mode;
  83. u8 rxd;
  84. u8 txd;
  85. u8 cr;
  86. u8 sr;
  87. u8 dummy;
  88. /*
  89. * Clock divisor modulus register
  90. * This uses the follwing formula:
  91. * SCPClkOut = OPBCLK/(4(CDM + 1))
  92. * or
  93. * CDM = (OPBCLK/4*SCPClkOut) - 1
  94. * bit 0 is the MSb!
  95. */
  96. u8 cdm;
  97. };
  98. /* SPI Controller driver's private data. */
  99. struct ppc4xx_spi {
  100. /* bitbang has to be first */
  101. struct spi_bitbang bitbang;
  102. struct completion done;
  103. u64 mapbase;
  104. u64 mapsize;
  105. int irqnum;
  106. /* need this to set the SPI clock */
  107. unsigned int opb_freq;
  108. /* for transfers */
  109. int len;
  110. int count;
  111. /* data buffers */
  112. const unsigned char *tx;
  113. unsigned char *rx;
  114. int *gpios;
  115. struct spi_ppc4xx_regs __iomem *regs; /* pointer to the registers */
  116. struct spi_master *master;
  117. struct device *dev;
  118. };
  119. /* need this so we can set the clock in the chipselect routine */
  120. struct spi_ppc4xx_cs {
  121. u8 mode;
  122. };
  123. static int spi_ppc4xx_txrx(struct spi_device *spi, struct spi_transfer *t)
  124. {
  125. struct ppc4xx_spi *hw;
  126. u8 data;
  127. dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",
  128. t->tx_buf, t->rx_buf, t->len);
  129. hw = spi_master_get_devdata(spi->master);
  130. hw->tx = t->tx_buf;
  131. hw->rx = t->rx_buf;
  132. hw->len = t->len;
  133. hw->count = 0;
  134. /* send the first byte */
  135. data = hw->tx ? hw->tx[0] : 0;
  136. out_8(&hw->regs->txd, data);
  137. out_8(&hw->regs->cr, SPI_PPC4XX_CR_STR);
  138. wait_for_completion(&hw->done);
  139. return hw->count;
  140. }
  141. static int spi_ppc4xx_setupxfer(struct spi_device *spi, struct spi_transfer *t)
  142. {
  143. struct ppc4xx_spi *hw = spi_master_get_devdata(spi->master);
  144. struct spi_ppc4xx_cs *cs = spi->controller_state;
  145. int scr;
  146. u8 cdm = 0;
  147. u32 speed;
  148. u8 bits_per_word;
  149. /* Start with the generic configuration for this device. */
  150. bits_per_word = spi->bits_per_word;
  151. speed = spi->max_speed_hz;
  152. /*
  153. * Modify the configuration if the transfer overrides it. Do not allow
  154. * the transfer to overwrite the generic configuration with zeros.
  155. */
  156. if (t) {
  157. if (t->bits_per_word)
  158. bits_per_word = t->bits_per_word;
  159. if (t->speed_hz)
  160. speed = min(t->speed_hz, spi->max_speed_hz);
  161. }
  162. if (bits_per_word != 8) {
  163. dev_err(&spi->dev, "invalid bits-per-word (%d)\n",
  164. bits_per_word);
  165. return -EINVAL;
  166. }
  167. if (!speed || (speed > spi->max_speed_hz)) {
  168. dev_err(&spi->dev, "invalid speed_hz (%d)\n", speed);
  169. return -EINVAL;
  170. }
  171. /* Write new configration */
  172. out_8(&hw->regs->mode, cs->mode);
  173. /* Set the clock */
  174. /* opb_freq was already divided by 4 */
  175. scr = (hw->opb_freq / speed) - 1;
  176. if (scr > 0)
  177. cdm = min(scr, 0xff);
  178. dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", cdm, speed);
  179. if (in_8(&hw->regs->cdm) != cdm)
  180. out_8(&hw->regs->cdm, cdm);
  181. spin_lock(&hw->bitbang.lock);
  182. if (!hw->bitbang.busy) {
  183. hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
  184. /* Need to ndelay here? */
  185. }
  186. spin_unlock(&hw->bitbang.lock);
  187. return 0;
  188. }
  189. static int spi_ppc4xx_setup(struct spi_device *spi)
  190. {
  191. struct spi_ppc4xx_cs *cs = spi->controller_state;
  192. if (spi->bits_per_word != 8) {
  193. dev_err(&spi->dev, "invalid bits-per-word (%d)\n",
  194. spi->bits_per_word);
  195. return -EINVAL;
  196. }
  197. if (!spi->max_speed_hz) {
  198. dev_err(&spi->dev, "invalid max_speed_hz (must be non-zero)\n");
  199. return -EINVAL;
  200. }
  201. if (cs == NULL) {
  202. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  203. if (!cs)
  204. return -ENOMEM;
  205. spi->controller_state = cs;
  206. }
  207. /*
  208. * We set all bits of the SPI0_MODE register, so,
  209. * no need to read-modify-write
  210. */
  211. cs->mode = SPI_PPC4XX_MODE_SPE;
  212. switch (spi->mode & (SPI_CPHA | SPI_CPOL)) {
  213. case SPI_MODE_0:
  214. cs->mode |= SPI_CLK_MODE0;
  215. break;
  216. case SPI_MODE_1:
  217. cs->mode |= SPI_CLK_MODE1;
  218. break;
  219. case SPI_MODE_2:
  220. cs->mode |= SPI_CLK_MODE2;
  221. break;
  222. case SPI_MODE_3:
  223. cs->mode |= SPI_CLK_MODE3;
  224. break;
  225. }
  226. if (spi->mode & SPI_LSB_FIRST)
  227. cs->mode |= SPI_PPC4XX_MODE_RD;
  228. return 0;
  229. }
  230. static void spi_ppc4xx_chipsel(struct spi_device *spi, int value)
  231. {
  232. struct ppc4xx_spi *hw = spi_master_get_devdata(spi->master);
  233. unsigned int cs = spi->chip_select;
  234. unsigned int cspol;
  235. /*
  236. * If there are no chip selects at all, or if this is the special
  237. * case of a non-existent (dummy) chip select, do nothing.
  238. */
  239. if (!hw->master->num_chipselect || hw->gpios[cs] == -EEXIST)
  240. return;
  241. cspol = spi->mode & SPI_CS_HIGH ? 1 : 0;
  242. if (value == BITBANG_CS_INACTIVE)
  243. cspol = !cspol;
  244. gpio_set_value(hw->gpios[cs], cspol);
  245. }
  246. static irqreturn_t spi_ppc4xx_int(int irq, void *dev_id)
  247. {
  248. struct ppc4xx_spi *hw;
  249. u8 status;
  250. u8 data;
  251. unsigned int count;
  252. hw = (struct ppc4xx_spi *)dev_id;
  253. status = in_8(&hw->regs->sr);
  254. if (!status)
  255. return IRQ_NONE;
  256. /*
  257. * BSY de-asserts one cycle after the transfer is complete. The
  258. * interrupt is asserted after the transfer is complete. The exact
  259. * relationship is not documented, hence this code.
  260. */
  261. if (unlikely(status & SPI_PPC4XX_SR_BSY)) {
  262. u8 lstatus;
  263. int cnt = 0;
  264. dev_dbg(hw->dev, "got interrupt but spi still busy?\n");
  265. do {
  266. ndelay(10);
  267. lstatus = in_8(&hw->regs->sr);
  268. } while (++cnt < 100 && lstatus & SPI_PPC4XX_SR_BSY);
  269. if (cnt >= 100) {
  270. dev_err(hw->dev, "busywait: too many loops!\n");
  271. complete(&hw->done);
  272. return IRQ_HANDLED;
  273. } else {
  274. /* status is always 1 (RBR) here */
  275. status = in_8(&hw->regs->sr);
  276. dev_dbg(hw->dev, "loops %d status %x\n", cnt, status);
  277. }
  278. }
  279. count = hw->count;
  280. hw->count++;
  281. /* RBR triggered this interrupt. Therefore, data must be ready. */
  282. data = in_8(&hw->regs->rxd);
  283. if (hw->rx)
  284. hw->rx[count] = data;
  285. count++;
  286. if (count < hw->len) {
  287. data = hw->tx ? hw->tx[count] : 0;
  288. out_8(&hw->regs->txd, data);
  289. out_8(&hw->regs->cr, SPI_PPC4XX_CR_STR);
  290. } else {
  291. complete(&hw->done);
  292. }
  293. return IRQ_HANDLED;
  294. }
  295. static void spi_ppc4xx_cleanup(struct spi_device *spi)
  296. {
  297. kfree(spi->controller_state);
  298. }
  299. static void spi_ppc4xx_enable(struct ppc4xx_spi *hw)
  300. {
  301. /*
  302. * On all 4xx PPC's the SPI bus is shared/multiplexed with
  303. * the 2nd I2C bus. We need to enable the the SPI bus before
  304. * using it.
  305. */
  306. /* need to clear bit 14 to enable SPC */
  307. dcri_clrset(SDR0, SDR0_PFC1, 0x80000000 >> 14, 0);
  308. }
  309. static void free_gpios(struct ppc4xx_spi *hw)
  310. {
  311. if (hw->master->num_chipselect) {
  312. int i;
  313. for (i = 0; i < hw->master->num_chipselect; i++)
  314. if (gpio_is_valid(hw->gpios[i]))
  315. gpio_free(hw->gpios[i]);
  316. kfree(hw->gpios);
  317. hw->gpios = NULL;
  318. }
  319. }
  320. /*
  321. * platform_device layer stuff...
  322. */
  323. static int __init spi_ppc4xx_of_probe(struct platform_device *op)
  324. {
  325. struct ppc4xx_spi *hw;
  326. struct spi_master *master;
  327. struct spi_bitbang *bbp;
  328. struct resource resource;
  329. struct device_node *np = op->dev.of_node;
  330. struct device *dev = &op->dev;
  331. struct device_node *opbnp;
  332. int ret;
  333. int num_gpios;
  334. const unsigned int *clk;
  335. master = spi_alloc_master(dev, sizeof *hw);
  336. if (master == NULL)
  337. return -ENOMEM;
  338. master->dev.of_node = np;
  339. dev_set_drvdata(dev, master);
  340. hw = spi_master_get_devdata(master);
  341. hw->master = spi_master_get(master);
  342. hw->dev = dev;
  343. init_completion(&hw->done);
  344. /*
  345. * A count of zero implies a single SPI device without any chip-select.
  346. * Note that of_gpio_count counts all gpios assigned to this spi master.
  347. * This includes both "null" gpio's and real ones.
  348. */
  349. num_gpios = of_gpio_count(np);
  350. if (num_gpios) {
  351. int i;
  352. hw->gpios = kzalloc(sizeof(int) * num_gpios, GFP_KERNEL);
  353. if (!hw->gpios) {
  354. ret = -ENOMEM;
  355. goto free_master;
  356. }
  357. for (i = 0; i < num_gpios; i++) {
  358. int gpio;
  359. enum of_gpio_flags flags;
  360. gpio = of_get_gpio_flags(np, i, &flags);
  361. hw->gpios[i] = gpio;
  362. if (gpio_is_valid(gpio)) {
  363. /* Real CS - set the initial state. */
  364. ret = gpio_request(gpio, np->name);
  365. if (ret < 0) {
  366. dev_err(dev, "can't request gpio "
  367. "#%d: %d\n", i, ret);
  368. goto free_gpios;
  369. }
  370. gpio_direction_output(gpio,
  371. !!(flags & OF_GPIO_ACTIVE_LOW));
  372. } else if (gpio == -EEXIST) {
  373. ; /* No CS, but that's OK. */
  374. } else {
  375. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  376. ret = -EINVAL;
  377. goto free_gpios;
  378. }
  379. }
  380. }
  381. /* Setup the state for the bitbang driver */
  382. bbp = &hw->bitbang;
  383. bbp->master = hw->master;
  384. bbp->setup_transfer = spi_ppc4xx_setupxfer;
  385. bbp->chipselect = spi_ppc4xx_chipsel;
  386. bbp->txrx_bufs = spi_ppc4xx_txrx;
  387. bbp->use_dma = 0;
  388. bbp->master->setup = spi_ppc4xx_setup;
  389. bbp->master->cleanup = spi_ppc4xx_cleanup;
  390. /* Allocate bus num dynamically. */
  391. bbp->master->bus_num = -1;
  392. /* the spi->mode bits understood by this driver: */
  393. bbp->master->mode_bits =
  394. SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST;
  395. /* this many pins in all GPIO controllers */
  396. bbp->master->num_chipselect = num_gpios;
  397. /* Get the clock for the OPB */
  398. opbnp = of_find_compatible_node(NULL, NULL, "ibm,opb");
  399. if (opbnp == NULL) {
  400. dev_err(dev, "OPB: cannot find node\n");
  401. ret = -ENODEV;
  402. goto free_gpios;
  403. }
  404. /* Get the clock (Hz) for the OPB */
  405. clk = of_get_property(opbnp, "clock-frequency", NULL);
  406. if (clk == NULL) {
  407. dev_err(dev, "OPB: no clock-frequency property set\n");
  408. of_node_put(opbnp);
  409. ret = -ENODEV;
  410. goto free_gpios;
  411. }
  412. hw->opb_freq = *clk;
  413. hw->opb_freq >>= 2;
  414. of_node_put(opbnp);
  415. ret = of_address_to_resource(np, 0, &resource);
  416. if (ret) {
  417. dev_err(dev, "error while parsing device node resource\n");
  418. goto free_gpios;
  419. }
  420. hw->mapbase = resource.start;
  421. hw->mapsize = resource.end - resource.start + 1;
  422. /* Sanity check */
  423. if (hw->mapsize < sizeof(struct spi_ppc4xx_regs)) {
  424. dev_err(dev, "too small to map registers\n");
  425. ret = -EINVAL;
  426. goto free_gpios;
  427. }
  428. /* Request IRQ */
  429. hw->irqnum = irq_of_parse_and_map(np, 0);
  430. ret = request_irq(hw->irqnum, spi_ppc4xx_int,
  431. IRQF_DISABLED, "spi_ppc4xx_of", (void *)hw);
  432. if (ret) {
  433. dev_err(dev, "unable to allocate interrupt\n");
  434. goto free_gpios;
  435. }
  436. if (!request_mem_region(hw->mapbase, hw->mapsize, DRIVER_NAME)) {
  437. dev_err(dev, "resource unavailable\n");
  438. ret = -EBUSY;
  439. goto request_mem_error;
  440. }
  441. hw->regs = ioremap(hw->mapbase, sizeof(struct spi_ppc4xx_regs));
  442. if (!hw->regs) {
  443. dev_err(dev, "unable to memory map registers\n");
  444. ret = -ENXIO;
  445. goto map_io_error;
  446. }
  447. spi_ppc4xx_enable(hw);
  448. /* Finally register our spi controller */
  449. dev->dma_mask = 0;
  450. ret = spi_bitbang_start(bbp);
  451. if (ret) {
  452. dev_err(dev, "failed to register SPI master\n");
  453. goto unmap_regs;
  454. }
  455. dev_info(dev, "driver initialized\n");
  456. return 0;
  457. unmap_regs:
  458. iounmap(hw->regs);
  459. map_io_error:
  460. release_mem_region(hw->mapbase, hw->mapsize);
  461. request_mem_error:
  462. free_irq(hw->irqnum, hw);
  463. free_gpios:
  464. free_gpios(hw);
  465. free_master:
  466. dev_set_drvdata(dev, NULL);
  467. spi_master_put(master);
  468. dev_err(dev, "initialization failed\n");
  469. return ret;
  470. }
  471. static int __exit spi_ppc4xx_of_remove(struct platform_device *op)
  472. {
  473. struct spi_master *master = dev_get_drvdata(&op->dev);
  474. struct ppc4xx_spi *hw = spi_master_get_devdata(master);
  475. spi_bitbang_stop(&hw->bitbang);
  476. dev_set_drvdata(&op->dev, NULL);
  477. release_mem_region(hw->mapbase, hw->mapsize);
  478. free_irq(hw->irqnum, hw);
  479. iounmap(hw->regs);
  480. free_gpios(hw);
  481. return 0;
  482. }
  483. static const struct of_device_id spi_ppc4xx_of_match[] = {
  484. { .compatible = "ibm,ppc4xx-spi", },
  485. {},
  486. };
  487. MODULE_DEVICE_TABLE(of, spi_ppc4xx_of_match);
  488. static struct platform_driver spi_ppc4xx_of_driver = {
  489. .probe = spi_ppc4xx_of_probe,
  490. .remove = __exit_p(spi_ppc4xx_of_remove),
  491. .driver = {
  492. .name = DRIVER_NAME,
  493. .owner = THIS_MODULE,
  494. .of_match_table = spi_ppc4xx_of_match,
  495. },
  496. };
  497. static int __init spi_ppc4xx_init(void)
  498. {
  499. return platform_driver_register(&spi_ppc4xx_of_driver);
  500. }
  501. module_init(spi_ppc4xx_init);
  502. static void __exit spi_ppc4xx_exit(void)
  503. {
  504. platform_driver_unregister(&spi_ppc4xx_of_driver);
  505. }
  506. module_exit(spi_ppc4xx_exit);
  507. MODULE_AUTHOR("Gary Jennejohn & Stefan Roese");
  508. MODULE_DESCRIPTION("Simple PPC4xx SPI Driver");
  509. MODULE_LICENSE("GPL");