spi-ppc4xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 = kcalloc(num_gpios, sizeof(*hw->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,
  357. "can't request gpio #%d: %d\n",
  358. 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. spi_master_put(master);
  470. return 0;
  471. }
  472. static const struct of_device_id spi_ppc4xx_of_match[] = {
  473. { .compatible = "ibm,ppc4xx-spi", },
  474. {},
  475. };
  476. MODULE_DEVICE_TABLE(of, spi_ppc4xx_of_match);
  477. static struct platform_driver spi_ppc4xx_of_driver = {
  478. .probe = spi_ppc4xx_of_probe,
  479. .remove = spi_ppc4xx_of_remove,
  480. .driver = {
  481. .name = DRIVER_NAME,
  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");