spi-ppc4xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/errno.h>
  28. #include <linux/wait.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/of_platform.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 following 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 (!speed || (speed > spi->max_speed_hz)) {
  163. dev_err(&spi->dev, "invalid speed_hz (%d)\n", speed);
  164. return -EINVAL;
  165. }
  166. /* Write new configuration */
  167. out_8(&hw->regs->mode, cs->mode);
  168. /* Set the clock */
  169. /* opb_freq was already divided by 4 */
  170. scr = (hw->opb_freq / speed) - 1;
  171. if (scr > 0)
  172. cdm = min(scr, 0xff);
  173. dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", cdm, speed);
  174. if (in_8(&hw->regs->cdm) != cdm)
  175. out_8(&hw->regs->cdm, cdm);
  176. mutex_lock(&hw->bitbang.lock);
  177. if (!hw->bitbang.busy) {
  178. hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
  179. /* Need to ndelay here? */
  180. }
  181. mutex_unlock(&hw->bitbang.lock);
  182. return 0;
  183. }
  184. static int spi_ppc4xx_setup(struct spi_device *spi)
  185. {
  186. struct spi_ppc4xx_cs *cs = spi->controller_state;
  187. if (!spi->max_speed_hz) {
  188. dev_err(&spi->dev, "invalid max_speed_hz (must be non-zero)\n");
  189. return -EINVAL;
  190. }
  191. if (cs == NULL) {
  192. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  193. if (!cs)
  194. return -ENOMEM;
  195. spi->controller_state = cs;
  196. }
  197. /*
  198. * We set all bits of the SPI0_MODE register, so,
  199. * no need to read-modify-write
  200. */
  201. cs->mode = SPI_PPC4XX_MODE_SPE;
  202. switch (spi->mode & (SPI_CPHA | SPI_CPOL)) {
  203. case SPI_MODE_0:
  204. cs->mode |= SPI_CLK_MODE0;
  205. break;
  206. case SPI_MODE_1:
  207. cs->mode |= SPI_CLK_MODE1;
  208. break;
  209. case SPI_MODE_2:
  210. cs->mode |= SPI_CLK_MODE2;
  211. break;
  212. case SPI_MODE_3:
  213. cs->mode |= SPI_CLK_MODE3;
  214. break;
  215. }
  216. if (spi->mode & SPI_LSB_FIRST)
  217. cs->mode |= SPI_PPC4XX_MODE_RD;
  218. return 0;
  219. }
  220. static void spi_ppc4xx_chipsel(struct spi_device *spi, int value)
  221. {
  222. struct ppc4xx_spi *hw = spi_master_get_devdata(spi->master);
  223. unsigned int cs = spi->chip_select;
  224. unsigned int cspol;
  225. /*
  226. * If there are no chip selects at all, or if this is the special
  227. * case of a non-existent (dummy) chip select, do nothing.
  228. */
  229. if (!hw->master->num_chipselect || hw->gpios[cs] == -EEXIST)
  230. return;
  231. cspol = spi->mode & SPI_CS_HIGH ? 1 : 0;
  232. if (value == BITBANG_CS_INACTIVE)
  233. cspol = !cspol;
  234. gpio_set_value(hw->gpios[cs], cspol);
  235. }
  236. static irqreturn_t spi_ppc4xx_int(int irq, void *dev_id)
  237. {
  238. struct ppc4xx_spi *hw;
  239. u8 status;
  240. u8 data;
  241. unsigned int count;
  242. hw = (struct ppc4xx_spi *)dev_id;
  243. status = in_8(&hw->regs->sr);
  244. if (!status)
  245. return IRQ_NONE;
  246. /*
  247. * BSY de-asserts one cycle after the transfer is complete. The
  248. * interrupt is asserted after the transfer is complete. The exact
  249. * relationship is not documented, hence this code.
  250. */
  251. if (unlikely(status & SPI_PPC4XX_SR_BSY)) {
  252. u8 lstatus;
  253. int cnt = 0;
  254. dev_dbg(hw->dev, "got interrupt but spi still busy?\n");
  255. do {
  256. ndelay(10);
  257. lstatus = in_8(&hw->regs->sr);
  258. } while (++cnt < 100 && lstatus & SPI_PPC4XX_SR_BSY);
  259. if (cnt >= 100) {
  260. dev_err(hw->dev, "busywait: too many loops!\n");
  261. complete(&hw->done);
  262. return IRQ_HANDLED;
  263. } else {
  264. /* status is always 1 (RBR) here */
  265. status = in_8(&hw->regs->sr);
  266. dev_dbg(hw->dev, "loops %d status %x\n", cnt, status);
  267. }
  268. }
  269. count = hw->count;
  270. hw->count++;
  271. /* RBR triggered this interrupt. Therefore, data must be ready. */
  272. data = in_8(&hw->regs->rxd);
  273. if (hw->rx)
  274. hw->rx[count] = data;
  275. count++;
  276. if (count < hw->len) {
  277. data = hw->tx ? hw->tx[count] : 0;
  278. out_8(&hw->regs->txd, data);
  279. out_8(&hw->regs->cr, SPI_PPC4XX_CR_STR);
  280. } else {
  281. complete(&hw->done);
  282. }
  283. return IRQ_HANDLED;
  284. }
  285. static void spi_ppc4xx_cleanup(struct spi_device *spi)
  286. {
  287. kfree(spi->controller_state);
  288. }
  289. static void spi_ppc4xx_enable(struct ppc4xx_spi *hw)
  290. {
  291. /*
  292. * On all 4xx PPC's the SPI bus is shared/multiplexed with
  293. * the 2nd I2C bus. We need to enable the the SPI bus before
  294. * using it.
  295. */
  296. /* need to clear bit 14 to enable SPC */
  297. dcri_clrset(SDR0, SDR0_PFC1, 0x80000000 >> 14, 0);
  298. }
  299. static void free_gpios(struct ppc4xx_spi *hw)
  300. {
  301. if (hw->master->num_chipselect) {
  302. int i;
  303. for (i = 0; i < hw->master->num_chipselect; i++)
  304. if (gpio_is_valid(hw->gpios[i]))
  305. gpio_free(hw->gpios[i]);
  306. kfree(hw->gpios);
  307. hw->gpios = NULL;
  308. }
  309. }
  310. /*
  311. * platform_device layer stuff...
  312. */
  313. static int spi_ppc4xx_of_probe(struct platform_device *op)
  314. {
  315. struct ppc4xx_spi *hw;
  316. struct spi_master *master;
  317. struct spi_bitbang *bbp;
  318. struct resource resource;
  319. struct device_node *np = op->dev.of_node;
  320. struct device *dev = &op->dev;
  321. struct device_node *opbnp;
  322. int ret;
  323. int num_gpios;
  324. const unsigned int *clk;
  325. master = spi_alloc_master(dev, sizeof *hw);
  326. if (master == NULL)
  327. return -ENOMEM;
  328. master->dev.of_node = np;
  329. platform_set_drvdata(op, master);
  330. hw = spi_master_get_devdata(master);
  331. hw->master = master;
  332. hw->dev = dev;
  333. init_completion(&hw->done);
  334. /*
  335. * A count of zero implies a single SPI device without any chip-select.
  336. * Note that of_gpio_count counts all gpios assigned to this spi master.
  337. * This includes both "null" gpio's and real ones.
  338. */
  339. num_gpios = of_gpio_count(np);
  340. if (num_gpios > 0) {
  341. int i;
  342. hw->gpios = kzalloc(sizeof(int) * num_gpios, GFP_KERNEL);
  343. if (!hw->gpios) {
  344. ret = -ENOMEM;
  345. goto free_master;
  346. }
  347. for (i = 0; i < num_gpios; i++) {
  348. int gpio;
  349. enum of_gpio_flags flags;
  350. gpio = of_get_gpio_flags(np, i, &flags);
  351. hw->gpios[i] = gpio;
  352. if (gpio_is_valid(gpio)) {
  353. /* Real CS - set the initial state. */
  354. ret = gpio_request(gpio, np->name);
  355. if (ret < 0) {
  356. dev_err(dev, "can't request gpio "
  357. "#%d: %d\n", i, ret);
  358. goto free_gpios;
  359. }
  360. gpio_direction_output(gpio,
  361. !!(flags & OF_GPIO_ACTIVE_LOW));
  362. } else if (gpio == -EEXIST) {
  363. ; /* No CS, but that's OK. */
  364. } else {
  365. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  366. ret = -EINVAL;
  367. goto free_gpios;
  368. }
  369. }
  370. }
  371. /* Setup the state for the bitbang driver */
  372. bbp = &hw->bitbang;
  373. bbp->master = hw->master;
  374. bbp->setup_transfer = spi_ppc4xx_setupxfer;
  375. bbp->chipselect = spi_ppc4xx_chipsel;
  376. bbp->txrx_bufs = spi_ppc4xx_txrx;
  377. bbp->use_dma = 0;
  378. bbp->master->setup = spi_ppc4xx_setup;
  379. bbp->master->cleanup = spi_ppc4xx_cleanup;
  380. bbp->master->bits_per_word_mask = SPI_BPW_MASK(8);
  381. /* the spi->mode bits understood by this driver: */
  382. bbp->master->mode_bits =
  383. SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST;
  384. /* this many pins in all GPIO controllers */
  385. bbp->master->num_chipselect = num_gpios > 0 ? num_gpios : 0;
  386. /* Get the clock for the OPB */
  387. opbnp = of_find_compatible_node(NULL, NULL, "ibm,opb");
  388. if (opbnp == NULL) {
  389. dev_err(dev, "OPB: cannot find node\n");
  390. ret = -ENODEV;
  391. goto free_gpios;
  392. }
  393. /* Get the clock (Hz) for the OPB */
  394. clk = of_get_property(opbnp, "clock-frequency", NULL);
  395. if (clk == NULL) {
  396. dev_err(dev, "OPB: no clock-frequency property set\n");
  397. of_node_put(opbnp);
  398. ret = -ENODEV;
  399. goto free_gpios;
  400. }
  401. hw->opb_freq = *clk;
  402. hw->opb_freq >>= 2;
  403. of_node_put(opbnp);
  404. ret = of_address_to_resource(np, 0, &resource);
  405. if (ret) {
  406. dev_err(dev, "error while parsing device node resource\n");
  407. goto free_gpios;
  408. }
  409. hw->mapbase = resource.start;
  410. hw->mapsize = resource_size(&resource);
  411. /* Sanity check */
  412. if (hw->mapsize < sizeof(struct spi_ppc4xx_regs)) {
  413. dev_err(dev, "too small to map registers\n");
  414. ret = -EINVAL;
  415. goto free_gpios;
  416. }
  417. /* Request IRQ */
  418. hw->irqnum = irq_of_parse_and_map(np, 0);
  419. ret = request_irq(hw->irqnum, spi_ppc4xx_int,
  420. 0, "spi_ppc4xx_of", (void *)hw);
  421. if (ret) {
  422. dev_err(dev, "unable to allocate interrupt\n");
  423. goto free_gpios;
  424. }
  425. if (!request_mem_region(hw->mapbase, hw->mapsize, DRIVER_NAME)) {
  426. dev_err(dev, "resource unavailable\n");
  427. ret = -EBUSY;
  428. goto request_mem_error;
  429. }
  430. hw->regs = ioremap(hw->mapbase, sizeof(struct spi_ppc4xx_regs));
  431. if (!hw->regs) {
  432. dev_err(dev, "unable to memory map registers\n");
  433. ret = -ENXIO;
  434. goto map_io_error;
  435. }
  436. spi_ppc4xx_enable(hw);
  437. /* Finally register our spi controller */
  438. dev->dma_mask = 0;
  439. ret = spi_bitbang_start(bbp);
  440. if (ret) {
  441. dev_err(dev, "failed to register SPI master\n");
  442. goto unmap_regs;
  443. }
  444. dev_info(dev, "driver initialized\n");
  445. return 0;
  446. unmap_regs:
  447. iounmap(hw->regs);
  448. map_io_error:
  449. release_mem_region(hw->mapbase, hw->mapsize);
  450. request_mem_error:
  451. free_irq(hw->irqnum, hw);
  452. free_gpios:
  453. free_gpios(hw);
  454. free_master:
  455. spi_master_put(master);
  456. dev_err(dev, "initialization failed\n");
  457. return ret;
  458. }
  459. static int spi_ppc4xx_of_remove(struct platform_device *op)
  460. {
  461. struct spi_master *master = platform_get_drvdata(op);
  462. struct ppc4xx_spi *hw = spi_master_get_devdata(master);
  463. spi_bitbang_stop(&hw->bitbang);
  464. release_mem_region(hw->mapbase, hw->mapsize);
  465. free_irq(hw->irqnum, hw);
  466. iounmap(hw->regs);
  467. free_gpios(hw);
  468. spi_master_put(master);
  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. .of_match_table = spi_ppc4xx_of_match,
  482. },
  483. };
  484. module_platform_driver(spi_ppc4xx_of_driver);
  485. MODULE_AUTHOR("Gary Jennejohn & Stefan Roese");
  486. MODULE_DESCRIPTION("Simple PPC4xx SPI Driver");
  487. MODULE_LICENSE("GPL");