spi-ppc4xx.c 14 KB

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