spi-ppc4xx.c 14 KB

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