spi-txx9.c 11 KB

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