spi-orion.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Marvell Orion SPI controller driver
  3. *
  4. * Author: Shadi Ammouri <shadi@marvell.com>
  5. * Copyright (C) 2007-2008 Marvell Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/delay.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/clk.h>
  21. #include <asm/unaligned.h>
  22. #define DRIVER_NAME "orion_spi"
  23. #define ORION_NUM_CHIPSELECTS 1 /* only one slave is supported*/
  24. #define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
  25. #define ORION_SPI_IF_CTRL_REG 0x00
  26. #define ORION_SPI_IF_CONFIG_REG 0x04
  27. #define ORION_SPI_DATA_OUT_REG 0x08
  28. #define ORION_SPI_DATA_IN_REG 0x0c
  29. #define ORION_SPI_INT_CAUSE_REG 0x10
  30. #define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
  31. #define ORION_SPI_CLK_PRESCALE_MASK 0x1F
  32. struct orion_spi {
  33. struct work_struct work;
  34. /* Lock access to transfer list. */
  35. spinlock_t lock;
  36. struct list_head msg_queue;
  37. struct spi_master *master;
  38. void __iomem *base;
  39. unsigned int max_speed;
  40. unsigned int min_speed;
  41. struct clk *clk;
  42. };
  43. static struct workqueue_struct *orion_spi_wq;
  44. static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
  45. {
  46. return orion_spi->base + reg;
  47. }
  48. static inline void
  49. orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
  50. {
  51. void __iomem *reg_addr = spi_reg(orion_spi, reg);
  52. u32 val;
  53. val = readl(reg_addr);
  54. val |= mask;
  55. writel(val, reg_addr);
  56. }
  57. static inline void
  58. orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
  59. {
  60. void __iomem *reg_addr = spi_reg(orion_spi, reg);
  61. u32 val;
  62. val = readl(reg_addr);
  63. val &= ~mask;
  64. writel(val, reg_addr);
  65. }
  66. static int orion_spi_set_transfer_size(struct orion_spi *orion_spi, int size)
  67. {
  68. if (size == 16) {
  69. orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
  70. ORION_SPI_IF_8_16_BIT_MODE);
  71. } else if (size == 8) {
  72. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
  73. ORION_SPI_IF_8_16_BIT_MODE);
  74. } else {
  75. pr_debug("Bad bits per word value %d (only 8 or 16 are "
  76. "allowed).\n", size);
  77. return -EINVAL;
  78. }
  79. return 0;
  80. }
  81. static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
  82. {
  83. u32 tclk_hz;
  84. u32 rate;
  85. u32 prescale;
  86. u32 reg;
  87. struct orion_spi *orion_spi;
  88. orion_spi = spi_master_get_devdata(spi->master);
  89. tclk_hz = clk_get_rate(orion_spi->clk);
  90. /*
  91. * the supported rates are: 4,6,8...30
  92. * round up as we look for equal or less speed
  93. */
  94. rate = DIV_ROUND_UP(tclk_hz, speed);
  95. rate = roundup(rate, 2);
  96. /* check if requested speed is too small */
  97. if (rate > 30)
  98. return -EINVAL;
  99. if (rate < 4)
  100. rate = 4;
  101. /* Convert the rate to SPI clock divisor value. */
  102. prescale = 0x10 + rate/2;
  103. reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  104. reg = ((reg & ~ORION_SPI_CLK_PRESCALE_MASK) | prescale);
  105. writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  106. return 0;
  107. }
  108. /*
  109. * called only when no transfer is active on the bus
  110. */
  111. static int
  112. orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  113. {
  114. struct orion_spi *orion_spi;
  115. unsigned int speed = spi->max_speed_hz;
  116. unsigned int bits_per_word = spi->bits_per_word;
  117. int rc;
  118. orion_spi = spi_master_get_devdata(spi->master);
  119. if ((t != NULL) && t->speed_hz)
  120. speed = t->speed_hz;
  121. if ((t != NULL) && t->bits_per_word)
  122. bits_per_word = t->bits_per_word;
  123. rc = orion_spi_baudrate_set(spi, speed);
  124. if (rc)
  125. return rc;
  126. return orion_spi_set_transfer_size(orion_spi, bits_per_word);
  127. }
  128. static void orion_spi_set_cs(struct orion_spi *orion_spi, int enable)
  129. {
  130. if (enable)
  131. orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  132. else
  133. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  134. }
  135. static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
  136. {
  137. int i;
  138. for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
  139. if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
  140. return 1;
  141. else
  142. udelay(1);
  143. }
  144. return -1;
  145. }
  146. static inline int
  147. orion_spi_write_read_8bit(struct spi_device *spi,
  148. const u8 **tx_buf, u8 **rx_buf)
  149. {
  150. void __iomem *tx_reg, *rx_reg, *int_reg;
  151. struct orion_spi *orion_spi;
  152. orion_spi = spi_master_get_devdata(spi->master);
  153. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  154. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  155. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  156. /* clear the interrupt cause register */
  157. writel(0x0, int_reg);
  158. if (tx_buf && *tx_buf)
  159. writel(*(*tx_buf)++, tx_reg);
  160. else
  161. writel(0, tx_reg);
  162. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  163. dev_err(&spi->dev, "TXS timed out\n");
  164. return -1;
  165. }
  166. if (rx_buf && *rx_buf)
  167. *(*rx_buf)++ = readl(rx_reg);
  168. return 1;
  169. }
  170. static inline int
  171. orion_spi_write_read_16bit(struct spi_device *spi,
  172. const u16 **tx_buf, u16 **rx_buf)
  173. {
  174. void __iomem *tx_reg, *rx_reg, *int_reg;
  175. struct orion_spi *orion_spi;
  176. orion_spi = spi_master_get_devdata(spi->master);
  177. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  178. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  179. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  180. /* clear the interrupt cause register */
  181. writel(0x0, int_reg);
  182. if (tx_buf && *tx_buf)
  183. writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
  184. else
  185. writel(0, tx_reg);
  186. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  187. dev_err(&spi->dev, "TXS timed out\n");
  188. return -1;
  189. }
  190. if (rx_buf && *rx_buf)
  191. put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
  192. return 1;
  193. }
  194. static unsigned int
  195. orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
  196. {
  197. struct orion_spi *orion_spi;
  198. unsigned int count;
  199. int word_len;
  200. orion_spi = spi_master_get_devdata(spi->master);
  201. word_len = spi->bits_per_word;
  202. count = xfer->len;
  203. if (word_len == 8) {
  204. const u8 *tx = xfer->tx_buf;
  205. u8 *rx = xfer->rx_buf;
  206. do {
  207. if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
  208. goto out;
  209. count--;
  210. } while (count);
  211. } else if (word_len == 16) {
  212. const u16 *tx = xfer->tx_buf;
  213. u16 *rx = xfer->rx_buf;
  214. do {
  215. if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
  216. goto out;
  217. count -= 2;
  218. } while (count);
  219. }
  220. out:
  221. return xfer->len - count;
  222. }
  223. static void orion_spi_work(struct work_struct *work)
  224. {
  225. struct orion_spi *orion_spi =
  226. container_of(work, struct orion_spi, work);
  227. spin_lock_irq(&orion_spi->lock);
  228. while (!list_empty(&orion_spi->msg_queue)) {
  229. struct spi_message *m;
  230. struct spi_device *spi;
  231. struct spi_transfer *t = NULL;
  232. int par_override = 0;
  233. int status = 0;
  234. int cs_active = 0;
  235. m = container_of(orion_spi->msg_queue.next, struct spi_message,
  236. queue);
  237. list_del_init(&m->queue);
  238. spin_unlock_irq(&orion_spi->lock);
  239. spi = m->spi;
  240. /* Load defaults */
  241. status = orion_spi_setup_transfer(spi, NULL);
  242. if (status < 0)
  243. goto msg_done;
  244. list_for_each_entry(t, &m->transfers, transfer_list) {
  245. if (par_override || t->speed_hz || t->bits_per_word) {
  246. par_override = 1;
  247. status = orion_spi_setup_transfer(spi, t);
  248. if (status < 0)
  249. break;
  250. if (!t->speed_hz && !t->bits_per_word)
  251. par_override = 0;
  252. }
  253. if (!cs_active) {
  254. orion_spi_set_cs(orion_spi, 1);
  255. cs_active = 1;
  256. }
  257. if (t->len)
  258. m->actual_length +=
  259. orion_spi_write_read(spi, t);
  260. if (t->delay_usecs)
  261. udelay(t->delay_usecs);
  262. if (t->cs_change) {
  263. orion_spi_set_cs(orion_spi, 0);
  264. cs_active = 0;
  265. }
  266. }
  267. msg_done:
  268. if (cs_active)
  269. orion_spi_set_cs(orion_spi, 0);
  270. m->status = status;
  271. m->complete(m->context);
  272. spin_lock_irq(&orion_spi->lock);
  273. }
  274. spin_unlock_irq(&orion_spi->lock);
  275. }
  276. static int __init orion_spi_reset(struct orion_spi *orion_spi)
  277. {
  278. /* Verify that the CS is deasserted */
  279. orion_spi_set_cs(orion_spi, 0);
  280. return 0;
  281. }
  282. static int orion_spi_setup(struct spi_device *spi)
  283. {
  284. struct orion_spi *orion_spi;
  285. orion_spi = spi_master_get_devdata(spi->master);
  286. if ((spi->max_speed_hz == 0)
  287. || (spi->max_speed_hz > orion_spi->max_speed))
  288. spi->max_speed_hz = orion_spi->max_speed;
  289. if (spi->max_speed_hz < orion_spi->min_speed) {
  290. dev_err(&spi->dev, "setup: requested speed too low %d Hz\n",
  291. spi->max_speed_hz);
  292. return -EINVAL;
  293. }
  294. /*
  295. * baudrate & width will be set orion_spi_setup_transfer
  296. */
  297. return 0;
  298. }
  299. static int orion_spi_transfer(struct spi_device *spi, struct spi_message *m)
  300. {
  301. struct orion_spi *orion_spi;
  302. struct spi_transfer *t = NULL;
  303. unsigned long flags;
  304. m->actual_length = 0;
  305. m->status = 0;
  306. /* reject invalid messages and transfers */
  307. if (list_empty(&m->transfers) || !m->complete)
  308. return -EINVAL;
  309. orion_spi = spi_master_get_devdata(spi->master);
  310. list_for_each_entry(t, &m->transfers, transfer_list) {
  311. unsigned int bits_per_word = spi->bits_per_word;
  312. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  313. dev_err(&spi->dev,
  314. "message rejected : "
  315. "invalid transfer data buffers\n");
  316. goto msg_rejected;
  317. }
  318. if (t->bits_per_word)
  319. bits_per_word = t->bits_per_word;
  320. if ((bits_per_word != 8) && (bits_per_word != 16)) {
  321. dev_err(&spi->dev,
  322. "message rejected : "
  323. "invalid transfer bits_per_word (%d bits)\n",
  324. bits_per_word);
  325. goto msg_rejected;
  326. }
  327. /*make sure buffer length is even when working in 16 bit mode*/
  328. if ((t->bits_per_word == 16) && (t->len & 1)) {
  329. dev_err(&spi->dev,
  330. "message rejected : "
  331. "odd data length (%d) while in 16 bit mode\n",
  332. t->len);
  333. goto msg_rejected;
  334. }
  335. if (t->speed_hz && t->speed_hz < orion_spi->min_speed) {
  336. dev_err(&spi->dev,
  337. "message rejected : "
  338. "device min speed (%d Hz) exceeds "
  339. "required transfer speed (%d Hz)\n",
  340. orion_spi->min_speed, t->speed_hz);
  341. goto msg_rejected;
  342. }
  343. }
  344. spin_lock_irqsave(&orion_spi->lock, flags);
  345. list_add_tail(&m->queue, &orion_spi->msg_queue);
  346. queue_work(orion_spi_wq, &orion_spi->work);
  347. spin_unlock_irqrestore(&orion_spi->lock, flags);
  348. return 0;
  349. msg_rejected:
  350. /* Message rejected and not queued */
  351. m->status = -EINVAL;
  352. if (m->complete)
  353. m->complete(m->context);
  354. return -EINVAL;
  355. }
  356. static int __init orion_spi_probe(struct platform_device *pdev)
  357. {
  358. struct spi_master *master;
  359. struct orion_spi *spi;
  360. struct resource *r;
  361. unsigned long tclk_hz;
  362. int status = 0;
  363. const u32 *iprop;
  364. int size;
  365. master = spi_alloc_master(&pdev->dev, sizeof *spi);
  366. if (master == NULL) {
  367. dev_dbg(&pdev->dev, "master allocation failed\n");
  368. return -ENOMEM;
  369. }
  370. if (pdev->id != -1)
  371. master->bus_num = pdev->id;
  372. if (pdev->dev.of_node) {
  373. iprop = of_get_property(pdev->dev.of_node, "cell-index",
  374. &size);
  375. if (iprop && size == sizeof(*iprop))
  376. master->bus_num = *iprop;
  377. }
  378. /* we support only mode 0, and no options */
  379. master->mode_bits = 0;
  380. master->setup = orion_spi_setup;
  381. master->transfer = orion_spi_transfer;
  382. master->num_chipselect = ORION_NUM_CHIPSELECTS;
  383. dev_set_drvdata(&pdev->dev, master);
  384. spi = spi_master_get_devdata(master);
  385. spi->master = master;
  386. spi->clk = clk_get(&pdev->dev, NULL);
  387. if (IS_ERR(spi->clk)) {
  388. status = PTR_ERR(spi->clk);
  389. goto out;
  390. }
  391. clk_prepare(spi->clk);
  392. clk_enable(spi->clk);
  393. tclk_hz = clk_get_rate(spi->clk);
  394. spi->max_speed = DIV_ROUND_UP(tclk_hz, 4);
  395. spi->min_speed = DIV_ROUND_UP(tclk_hz, 30);
  396. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  397. if (r == NULL) {
  398. status = -ENODEV;
  399. goto out_rel_clk;
  400. }
  401. if (!request_mem_region(r->start, resource_size(r),
  402. dev_name(&pdev->dev))) {
  403. status = -EBUSY;
  404. goto out_rel_clk;
  405. }
  406. spi->base = ioremap(r->start, SZ_1K);
  407. INIT_WORK(&spi->work, orion_spi_work);
  408. spin_lock_init(&spi->lock);
  409. INIT_LIST_HEAD(&spi->msg_queue);
  410. if (orion_spi_reset(spi) < 0)
  411. goto out_rel_mem;
  412. master->dev.of_node = pdev->dev.of_node;
  413. status = spi_register_master(master);
  414. if (status < 0)
  415. goto out_rel_mem;
  416. return status;
  417. out_rel_mem:
  418. release_mem_region(r->start, resource_size(r));
  419. out_rel_clk:
  420. clk_disable_unprepare(spi->clk);
  421. clk_put(spi->clk);
  422. out:
  423. spi_master_put(master);
  424. return status;
  425. }
  426. static int __exit orion_spi_remove(struct platform_device *pdev)
  427. {
  428. struct spi_master *master;
  429. struct orion_spi *spi;
  430. struct resource *r;
  431. master = dev_get_drvdata(&pdev->dev);
  432. spi = spi_master_get_devdata(master);
  433. cancel_work_sync(&spi->work);
  434. clk_disable_unprepare(spi->clk);
  435. clk_put(spi->clk);
  436. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  437. release_mem_region(r->start, resource_size(r));
  438. spi_unregister_master(master);
  439. return 0;
  440. }
  441. MODULE_ALIAS("platform:" DRIVER_NAME);
  442. static const struct of_device_id orion_spi_of_match_table[] __devinitdata = {
  443. { .compatible = "marvell,orion-spi", },
  444. {}
  445. };
  446. MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
  447. static struct platform_driver orion_spi_driver = {
  448. .driver = {
  449. .name = DRIVER_NAME,
  450. .owner = THIS_MODULE,
  451. .of_match_table = of_match_ptr(orion_spi_of_match_table),
  452. },
  453. .remove = __exit_p(orion_spi_remove),
  454. };
  455. static int __init orion_spi_init(void)
  456. {
  457. orion_spi_wq = create_singlethread_workqueue(
  458. orion_spi_driver.driver.name);
  459. if (orion_spi_wq == NULL)
  460. return -ENOMEM;
  461. return platform_driver_probe(&orion_spi_driver, orion_spi_probe);
  462. }
  463. module_init(orion_spi_init);
  464. static void __exit orion_spi_exit(void)
  465. {
  466. flush_workqueue(orion_spi_wq);
  467. platform_driver_unregister(&orion_spi_driver);
  468. destroy_workqueue(orion_spi_wq);
  469. }
  470. module_exit(orion_spi_exit);
  471. MODULE_DESCRIPTION("Orion SPI driver");
  472. MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
  473. MODULE_LICENSE("GPL");