spi_txx9.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * spi_txx9.c - TXx9 SPI controller driver.
  3. *
  4. * Based on linux/arch/mips/tx4938/toshiba_rbtx4938/spi_txx9.c
  5. * Copyright (C) 2000-2001 Toshiba Corporation
  6. *
  7. * 2003-2005 (c) MontaVista Software, Inc. This file is licensed under the
  8. * terms of the GNU General Public License version 2. This program is
  9. * licensed "as is" without any warranty of any kind, whether express
  10. * or implied.
  11. *
  12. * Support for TX4938 in 2.6 - Manish Lachwani (mlachwani@mvista.com)
  13. *
  14. * Convert to generic SPI framework - Atsushi Nemoto (anemo@mba.ocn.ne.jp)
  15. */
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/errno.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/sched.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/err.h>
  26. #include <linux/clk.h>
  27. #include <linux/io.h>
  28. #include <asm/gpio.h>
  29. #define SPI_FIFO_SIZE 4
  30. #define SPI_MAX_DIVIDER 0xff /* Max. value for SPCR1.SER */
  31. #define SPI_MIN_DIVIDER 1 /* Min. value for SPCR1.SER */
  32. #define TXx9_SPMCR 0x00
  33. #define TXx9_SPCR0 0x04
  34. #define TXx9_SPCR1 0x08
  35. #define TXx9_SPFS 0x0c
  36. #define TXx9_SPSR 0x14
  37. #define TXx9_SPDR 0x18
  38. /* SPMCR : SPI Master Control */
  39. #define TXx9_SPMCR_OPMODE 0xc0
  40. #define TXx9_SPMCR_CONFIG 0x40
  41. #define TXx9_SPMCR_ACTIVE 0x80
  42. #define TXx9_SPMCR_SPSTP 0x02
  43. #define TXx9_SPMCR_BCLR 0x01
  44. /* SPCR0 : SPI Control 0 */
  45. #define TXx9_SPCR0_TXIFL_MASK 0xc000
  46. #define TXx9_SPCR0_RXIFL_MASK 0x3000
  47. #define TXx9_SPCR0_SIDIE 0x0800
  48. #define TXx9_SPCR0_SOEIE 0x0400
  49. #define TXx9_SPCR0_RBSIE 0x0200
  50. #define TXx9_SPCR0_TBSIE 0x0100
  51. #define TXx9_SPCR0_IFSPSE 0x0010
  52. #define TXx9_SPCR0_SBOS 0x0004
  53. #define TXx9_SPCR0_SPHA 0x0002
  54. #define TXx9_SPCR0_SPOL 0x0001
  55. /* SPSR : SPI Status */
  56. #define TXx9_SPSR_TBSI 0x8000
  57. #define TXx9_SPSR_RBSI 0x4000
  58. #define TXx9_SPSR_TBS_MASK 0x3800
  59. #define TXx9_SPSR_RBS_MASK 0x0700
  60. #define TXx9_SPSR_SPOE 0x0080
  61. #define TXx9_SPSR_IFSD 0x0008
  62. #define TXx9_SPSR_SIDLE 0x0004
  63. #define TXx9_SPSR_STRDY 0x0002
  64. #define TXx9_SPSR_SRRDY 0x0001
  65. struct txx9spi {
  66. struct workqueue_struct *workqueue;
  67. struct work_struct work;
  68. spinlock_t lock; /* protect 'queue' */
  69. struct list_head queue;
  70. wait_queue_head_t waitq;
  71. void __iomem *membase;
  72. int baseclk;
  73. struct clk *clk;
  74. u32 max_speed_hz, min_speed_hz;
  75. int last_chipselect;
  76. int last_chipselect_val;
  77. };
  78. static u32 txx9spi_rd(struct txx9spi *c, int reg)
  79. {
  80. return __raw_readl(c->membase + reg);
  81. }
  82. static void txx9spi_wr(struct txx9spi *c, u32 val, int reg)
  83. {
  84. __raw_writel(val, c->membase + reg);
  85. }
  86. static void txx9spi_cs_func(struct spi_device *spi, struct txx9spi *c,
  87. int on, unsigned int cs_delay)
  88. {
  89. int val = (spi->mode & SPI_CS_HIGH) ? on : !on;
  90. if (on) {
  91. /* deselect the chip with cs_change hint in last transfer */
  92. if (c->last_chipselect >= 0)
  93. gpio_set_value(c->last_chipselect,
  94. !c->last_chipselect_val);
  95. c->last_chipselect = spi->chip_select;
  96. c->last_chipselect_val = val;
  97. } else {
  98. c->last_chipselect = -1;
  99. ndelay(cs_delay); /* CS Hold Time */
  100. }
  101. gpio_set_value(spi->chip_select, val);
  102. ndelay(cs_delay); /* CS Setup Time / CS Recovery Time */
  103. }
  104. static int txx9spi_setup(struct spi_device *spi)
  105. {
  106. struct txx9spi *c = spi_master_get_devdata(spi->master);
  107. u8 bits_per_word;
  108. if (!spi->max_speed_hz
  109. || spi->max_speed_hz > c->max_speed_hz
  110. || spi->max_speed_hz < c->min_speed_hz)
  111. return -EINVAL;
  112. bits_per_word = spi->bits_per_word;
  113. if (bits_per_word != 8 && bits_per_word != 16)
  114. return -EINVAL;
  115. if (gpio_direction_output(spi->chip_select,
  116. !(spi->mode & SPI_CS_HIGH))) {
  117. dev_err(&spi->dev, "Cannot setup GPIO for chipselect.\n");
  118. return -EINVAL;
  119. }
  120. /* deselect chip */
  121. spin_lock(&c->lock);
  122. txx9spi_cs_func(spi, c, 0, (NSEC_PER_SEC / 2) / spi->max_speed_hz);
  123. spin_unlock(&c->lock);
  124. return 0;
  125. }
  126. static irqreturn_t txx9spi_interrupt(int irq, void *dev_id)
  127. {
  128. struct txx9spi *c = dev_id;
  129. /* disable rx intr */
  130. txx9spi_wr(c, txx9spi_rd(c, TXx9_SPCR0) & ~TXx9_SPCR0_RBSIE,
  131. TXx9_SPCR0);
  132. wake_up(&c->waitq);
  133. return IRQ_HANDLED;
  134. }
  135. static void txx9spi_work_one(struct txx9spi *c, struct spi_message *m)
  136. {
  137. struct spi_device *spi = m->spi;
  138. struct spi_transfer *t;
  139. unsigned int cs_delay;
  140. unsigned int cs_change = 1;
  141. int status = 0;
  142. u32 mcr;
  143. u32 prev_speed_hz = 0;
  144. u8 prev_bits_per_word = 0;
  145. /* CS setup/hold/recovery time in nsec */
  146. cs_delay = 100 + (NSEC_PER_SEC / 2) / spi->max_speed_hz;
  147. mcr = txx9spi_rd(c, TXx9_SPMCR);
  148. if (unlikely((mcr & TXx9_SPMCR_OPMODE) == TXx9_SPMCR_ACTIVE)) {
  149. dev_err(&spi->dev, "Bad mode.\n");
  150. status = -EIO;
  151. goto exit;
  152. }
  153. mcr &= ~(TXx9_SPMCR_OPMODE | TXx9_SPMCR_SPSTP | TXx9_SPMCR_BCLR);
  154. /* enter config mode */
  155. txx9spi_wr(c, mcr | TXx9_SPMCR_CONFIG | TXx9_SPMCR_BCLR, TXx9_SPMCR);
  156. txx9spi_wr(c, TXx9_SPCR0_SBOS
  157. | ((spi->mode & SPI_CPOL) ? TXx9_SPCR0_SPOL : 0)
  158. | ((spi->mode & SPI_CPHA) ? TXx9_SPCR0_SPHA : 0)
  159. | 0x08,
  160. TXx9_SPCR0);
  161. list_for_each_entry (t, &m->transfers, transfer_list) {
  162. const void *txbuf = t->tx_buf;
  163. void *rxbuf = t->rx_buf;
  164. u32 data;
  165. unsigned int len = t->len;
  166. unsigned int wsize;
  167. u32 speed_hz = t->speed_hz ? : spi->max_speed_hz;
  168. u8 bits_per_word = t->bits_per_word ? : spi->bits_per_word;
  169. bits_per_word = bits_per_word ? : 8;
  170. wsize = bits_per_word >> 3; /* in bytes */
  171. if (prev_speed_hz != speed_hz
  172. || prev_bits_per_word != bits_per_word) {
  173. int n = DIV_ROUND_UP(c->baseclk, speed_hz) - 1;
  174. n = clamp(n, SPI_MIN_DIVIDER, SPI_MAX_DIVIDER);
  175. /* enter config mode */
  176. txx9spi_wr(c, mcr | TXx9_SPMCR_CONFIG | TXx9_SPMCR_BCLR,
  177. TXx9_SPMCR);
  178. txx9spi_wr(c, (n << 8) | bits_per_word, TXx9_SPCR1);
  179. /* enter active mode */
  180. txx9spi_wr(c, mcr | TXx9_SPMCR_ACTIVE, TXx9_SPMCR);
  181. prev_speed_hz = speed_hz;
  182. prev_bits_per_word = bits_per_word;
  183. }
  184. if (cs_change)
  185. txx9spi_cs_func(spi, c, 1, cs_delay);
  186. cs_change = t->cs_change;
  187. while (len) {
  188. unsigned int count = SPI_FIFO_SIZE;
  189. int i;
  190. u32 cr0;
  191. if (len < count * wsize)
  192. count = len / wsize;
  193. /* now tx must be idle... */
  194. while (!(txx9spi_rd(c, TXx9_SPSR) & TXx9_SPSR_SIDLE))
  195. cpu_relax();
  196. cr0 = txx9spi_rd(c, TXx9_SPCR0);
  197. cr0 &= ~TXx9_SPCR0_RXIFL_MASK;
  198. cr0 |= (count - 1) << 12;
  199. /* enable rx intr */
  200. cr0 |= TXx9_SPCR0_RBSIE;
  201. txx9spi_wr(c, cr0, TXx9_SPCR0);
  202. /* send */
  203. for (i = 0; i < count; i++) {
  204. if (txbuf) {
  205. data = (wsize == 1)
  206. ? *(const u8 *)txbuf
  207. : *(const u16 *)txbuf;
  208. txx9spi_wr(c, data, TXx9_SPDR);
  209. txbuf += wsize;
  210. } else
  211. txx9spi_wr(c, 0, TXx9_SPDR);
  212. }
  213. /* wait all rx data */
  214. wait_event(c->waitq,
  215. txx9spi_rd(c, TXx9_SPSR) & TXx9_SPSR_RBSI);
  216. /* receive */
  217. for (i = 0; i < count; i++) {
  218. data = txx9spi_rd(c, TXx9_SPDR);
  219. if (rxbuf) {
  220. if (wsize == 1)
  221. *(u8 *)rxbuf = data;
  222. else
  223. *(u16 *)rxbuf = data;
  224. rxbuf += wsize;
  225. }
  226. }
  227. len -= count * wsize;
  228. }
  229. m->actual_length += t->len;
  230. if (t->delay_usecs)
  231. udelay(t->delay_usecs);
  232. if (!cs_change)
  233. continue;
  234. if (t->transfer_list.next == &m->transfers)
  235. break;
  236. /* sometimes a short mid-message deselect of the chip
  237. * may be needed to terminate a mode or command
  238. */
  239. txx9spi_cs_func(spi, c, 0, cs_delay);
  240. }
  241. exit:
  242. m->status = status;
  243. m->complete(m->context);
  244. /* normally deactivate chipselect ... unless no error and
  245. * cs_change has hinted that the next message will probably
  246. * be for this chip too.
  247. */
  248. if (!(status == 0 && cs_change))
  249. txx9spi_cs_func(spi, c, 0, cs_delay);
  250. /* enter config mode */
  251. txx9spi_wr(c, mcr | TXx9_SPMCR_CONFIG | TXx9_SPMCR_BCLR, TXx9_SPMCR);
  252. }
  253. static void txx9spi_work(struct work_struct *work)
  254. {
  255. struct txx9spi *c = container_of(work, struct txx9spi, work);
  256. unsigned long flags;
  257. spin_lock_irqsave(&c->lock, flags);
  258. while (!list_empty(&c->queue)) {
  259. struct spi_message *m;
  260. m = container_of(c->queue.next, struct spi_message, queue);
  261. list_del_init(&m->queue);
  262. spin_unlock_irqrestore(&c->lock, flags);
  263. txx9spi_work_one(c, m);
  264. spin_lock_irqsave(&c->lock, flags);
  265. }
  266. spin_unlock_irqrestore(&c->lock, flags);
  267. }
  268. static int txx9spi_transfer(struct spi_device *spi, struct spi_message *m)
  269. {
  270. struct spi_master *master = spi->master;
  271. struct txx9spi *c = spi_master_get_devdata(master);
  272. struct spi_transfer *t;
  273. unsigned long flags;
  274. m->actual_length = 0;
  275. /* check each transfer's parameters */
  276. list_for_each_entry (t, &m->transfers, transfer_list) {
  277. u32 speed_hz = t->speed_hz ? : spi->max_speed_hz;
  278. u8 bits_per_word = t->bits_per_word ? : spi->bits_per_word;
  279. bits_per_word = bits_per_word ? : 8;
  280. if (!t->tx_buf && !t->rx_buf && t->len)
  281. return -EINVAL;
  282. if (bits_per_word != 8 && bits_per_word != 16)
  283. return -EINVAL;
  284. if (t->len & ((bits_per_word >> 3) - 1))
  285. return -EINVAL;
  286. if (speed_hz < c->min_speed_hz || speed_hz > c->max_speed_hz)
  287. return -EINVAL;
  288. }
  289. spin_lock_irqsave(&c->lock, flags);
  290. list_add_tail(&m->queue, &c->queue);
  291. queue_work(c->workqueue, &c->work);
  292. spin_unlock_irqrestore(&c->lock, flags);
  293. return 0;
  294. }
  295. static int __init txx9spi_probe(struct platform_device *dev)
  296. {
  297. struct spi_master *master;
  298. struct txx9spi *c;
  299. struct resource *res;
  300. int ret = -ENODEV;
  301. u32 mcr;
  302. int irq;
  303. master = spi_alloc_master(&dev->dev, sizeof(*c));
  304. if (!master)
  305. return ret;
  306. c = spi_master_get_devdata(master);
  307. platform_set_drvdata(dev, master);
  308. INIT_WORK(&c->work, txx9spi_work);
  309. spin_lock_init(&c->lock);
  310. INIT_LIST_HEAD(&c->queue);
  311. init_waitqueue_head(&c->waitq);
  312. c->clk = clk_get(&dev->dev, "spi-baseclk");
  313. if (IS_ERR(c->clk)) {
  314. ret = PTR_ERR(c->clk);
  315. c->clk = NULL;
  316. goto exit;
  317. }
  318. ret = clk_enable(c->clk);
  319. if (ret) {
  320. clk_put(c->clk);
  321. c->clk = NULL;
  322. goto exit;
  323. }
  324. c->baseclk = clk_get_rate(c->clk);
  325. c->min_speed_hz = DIV_ROUND_UP(c->baseclk, SPI_MAX_DIVIDER + 1);
  326. c->max_speed_hz = c->baseclk / (SPI_MIN_DIVIDER + 1);
  327. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  328. if (!res)
  329. goto exit_busy;
  330. if (!devm_request_mem_region(&dev->dev, res->start, resource_size(res),
  331. "spi_txx9"))
  332. goto exit_busy;
  333. c->membase = devm_ioremap(&dev->dev, res->start, resource_size(res));
  334. if (!c->membase)
  335. goto exit_busy;
  336. /* enter config mode */
  337. mcr = txx9spi_rd(c, TXx9_SPMCR);
  338. mcr &= ~(TXx9_SPMCR_OPMODE | TXx9_SPMCR_SPSTP | TXx9_SPMCR_BCLR);
  339. txx9spi_wr(c, mcr | TXx9_SPMCR_CONFIG | TXx9_SPMCR_BCLR, TXx9_SPMCR);
  340. irq = platform_get_irq(dev, 0);
  341. if (irq < 0)
  342. goto exit_busy;
  343. ret = devm_request_irq(&dev->dev, irq, txx9spi_interrupt, 0,
  344. "spi_txx9", c);
  345. if (ret)
  346. goto exit;
  347. c->workqueue = create_singlethread_workqueue(
  348. dev_name(master->dev.parent));
  349. if (!c->workqueue)
  350. goto exit_busy;
  351. c->last_chipselect = -1;
  352. dev_info(&dev->dev, "at %#llx, irq %d, %dMHz\n",
  353. (unsigned long long)res->start, irq,
  354. (c->baseclk + 500000) / 1000000);
  355. /* the spi->mode bits understood by this driver: */
  356. master->mode_bits = SPI_CS_HIGH | SPI_CPOL | SPI_CPHA;
  357. master->bus_num = dev->id;
  358. master->setup = txx9spi_setup;
  359. master->transfer = txx9spi_transfer;
  360. master->num_chipselect = (u16)UINT_MAX; /* any GPIO numbers */
  361. ret = spi_register_master(master);
  362. if (ret)
  363. goto exit;
  364. return 0;
  365. exit_busy:
  366. ret = -EBUSY;
  367. exit:
  368. if (c->workqueue)
  369. destroy_workqueue(c->workqueue);
  370. if (c->clk) {
  371. clk_disable(c->clk);
  372. clk_put(c->clk);
  373. }
  374. platform_set_drvdata(dev, NULL);
  375. spi_master_put(master);
  376. return ret;
  377. }
  378. static int __exit txx9spi_remove(struct platform_device *dev)
  379. {
  380. struct spi_master *master = spi_master_get(platform_get_drvdata(dev));
  381. struct txx9spi *c = spi_master_get_devdata(master);
  382. spi_unregister_master(master);
  383. platform_set_drvdata(dev, NULL);
  384. destroy_workqueue(c->workqueue);
  385. clk_disable(c->clk);
  386. clk_put(c->clk);
  387. spi_master_put(master);
  388. return 0;
  389. }
  390. /* work with hotplug and coldplug */
  391. MODULE_ALIAS("platform:spi_txx9");
  392. static struct platform_driver txx9spi_driver = {
  393. .remove = __exit_p(txx9spi_remove),
  394. .driver = {
  395. .name = "spi_txx9",
  396. .owner = THIS_MODULE,
  397. },
  398. };
  399. static int __init txx9spi_init(void)
  400. {
  401. return platform_driver_probe(&txx9spi_driver, txx9spi_probe);
  402. }
  403. subsys_initcall(txx9spi_init);
  404. static void __exit txx9spi_exit(void)
  405. {
  406. platform_driver_unregister(&txx9spi_driver);
  407. }
  408. module_exit(txx9spi_exit);
  409. MODULE_DESCRIPTION("TXx9 SPI Driver");
  410. MODULE_LICENSE("GPL");