spi-ppc4xx.c 14 KB

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