spi-omap-100k.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * OMAP7xx SPI 100k controller driver
  3. * Author: Fabrice Crohas <fcrohas@gmail.com>
  4. * from original omap1_mcspi driver
  5. *
  6. * Copyright (C) 2005, 2006 Nokia Corporation
  7. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  8. * Juha Yrj�l� <juha.yrjola@nokia.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/module.h>
  29. #include <linux/device.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/err.h>
  33. #include <linux/clk.h>
  34. #include <linux/io.h>
  35. #include <linux/gpio.h>
  36. #include <linux/slab.h>
  37. #include <linux/spi/spi.h>
  38. #define OMAP1_SPI100K_MAX_FREQ 48000000
  39. #define ICR_SPITAS (OMAP7XX_ICR_BASE + 0x12)
  40. #define SPI_SETUP1 0x00
  41. #define SPI_SETUP2 0x02
  42. #define SPI_CTRL 0x04
  43. #define SPI_STATUS 0x06
  44. #define SPI_TX_LSB 0x08
  45. #define SPI_TX_MSB 0x0a
  46. #define SPI_RX_LSB 0x0c
  47. #define SPI_RX_MSB 0x0e
  48. #define SPI_SETUP1_INT_READ_ENABLE (1UL << 5)
  49. #define SPI_SETUP1_INT_WRITE_ENABLE (1UL << 4)
  50. #define SPI_SETUP1_CLOCK_DIVISOR(x) ((x) << 1)
  51. #define SPI_SETUP1_CLOCK_ENABLE (1UL << 0)
  52. #define SPI_SETUP2_ACTIVE_EDGE_FALLING (0UL << 0)
  53. #define SPI_SETUP2_ACTIVE_EDGE_RISING (1UL << 0)
  54. #define SPI_SETUP2_NEGATIVE_LEVEL (0UL << 5)
  55. #define SPI_SETUP2_POSITIVE_LEVEL (1UL << 5)
  56. #define SPI_SETUP2_LEVEL_TRIGGER (0UL << 10)
  57. #define SPI_SETUP2_EDGE_TRIGGER (1UL << 10)
  58. #define SPI_CTRL_SEN(x) ((x) << 7)
  59. #define SPI_CTRL_WORD_SIZE(x) (((x) - 1) << 2)
  60. #define SPI_CTRL_WR (1UL << 1)
  61. #define SPI_CTRL_RD (1UL << 0)
  62. #define SPI_STATUS_WE (1UL << 1)
  63. #define SPI_STATUS_RD (1UL << 0)
  64. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  65. * cache operations; better heuristics consider wordsize and bitrate.
  66. */
  67. #define DMA_MIN_BYTES 8
  68. #define SPI_RUNNING 0
  69. #define SPI_SHUTDOWN 1
  70. struct omap1_spi100k {
  71. struct clk *ick;
  72. struct clk *fck;
  73. /* Virtual base address of the controller */
  74. void __iomem *base;
  75. };
  76. struct omap1_spi100k_cs {
  77. void __iomem *base;
  78. int word_len;
  79. };
  80. static void spi100k_enable_clock(struct spi_master *master)
  81. {
  82. unsigned int val;
  83. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  84. /* enable SPI */
  85. val = readw(spi100k->base + SPI_SETUP1);
  86. val |= SPI_SETUP1_CLOCK_ENABLE;
  87. writew(val, spi100k->base + SPI_SETUP1);
  88. }
  89. static void spi100k_disable_clock(struct spi_master *master)
  90. {
  91. unsigned int val;
  92. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  93. /* disable SPI */
  94. val = readw(spi100k->base + SPI_SETUP1);
  95. val &= ~SPI_SETUP1_CLOCK_ENABLE;
  96. writew(val, spi100k->base + SPI_SETUP1);
  97. }
  98. static void spi100k_write_data(struct spi_master *master, int len, int data)
  99. {
  100. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  101. /* write 16-bit word, shifting 8-bit data if necessary */
  102. if (len <= 8) {
  103. data <<= 8;
  104. len = 16;
  105. }
  106. spi100k_enable_clock(master);
  107. writew(data , spi100k->base + SPI_TX_MSB);
  108. writew(SPI_CTRL_SEN(0) |
  109. SPI_CTRL_WORD_SIZE(len) |
  110. SPI_CTRL_WR,
  111. spi100k->base + SPI_CTRL);
  112. /* Wait for bit ack send change */
  113. while ((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_WE) != SPI_STATUS_WE)
  114. ;
  115. udelay(1000);
  116. spi100k_disable_clock(master);
  117. }
  118. static int spi100k_read_data(struct spi_master *master, int len)
  119. {
  120. int dataH, dataL;
  121. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  122. /* Always do at least 16 bits */
  123. if (len <= 8)
  124. len = 16;
  125. spi100k_enable_clock(master);
  126. writew(SPI_CTRL_SEN(0) |
  127. SPI_CTRL_WORD_SIZE(len) |
  128. SPI_CTRL_RD,
  129. spi100k->base + SPI_CTRL);
  130. while ((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_RD) != SPI_STATUS_RD)
  131. ;
  132. udelay(1000);
  133. dataL = readw(spi100k->base + SPI_RX_LSB);
  134. dataH = readw(spi100k->base + SPI_RX_MSB);
  135. spi100k_disable_clock(master);
  136. return dataL;
  137. }
  138. static void spi100k_open(struct spi_master *master)
  139. {
  140. /* get control of SPI */
  141. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  142. writew(SPI_SETUP1_INT_READ_ENABLE |
  143. SPI_SETUP1_INT_WRITE_ENABLE |
  144. SPI_SETUP1_CLOCK_DIVISOR(0), spi100k->base + SPI_SETUP1);
  145. /* configure clock and interrupts */
  146. writew(SPI_SETUP2_ACTIVE_EDGE_FALLING |
  147. SPI_SETUP2_NEGATIVE_LEVEL |
  148. SPI_SETUP2_LEVEL_TRIGGER, spi100k->base + SPI_SETUP2);
  149. }
  150. static void omap1_spi100k_force_cs(struct omap1_spi100k *spi100k, int enable)
  151. {
  152. if (enable)
  153. writew(0x05fc, spi100k->base + SPI_CTRL);
  154. else
  155. writew(0x05fd, spi100k->base + SPI_CTRL);
  156. }
  157. static unsigned
  158. omap1_spi100k_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  159. {
  160. struct omap1_spi100k_cs *cs = spi->controller_state;
  161. unsigned int count, c;
  162. int word_len;
  163. count = xfer->len;
  164. c = count;
  165. word_len = cs->word_len;
  166. if (word_len <= 8) {
  167. u8 *rx;
  168. const u8 *tx;
  169. rx = xfer->rx_buf;
  170. tx = xfer->tx_buf;
  171. do {
  172. c -= 1;
  173. if (xfer->tx_buf != NULL)
  174. spi100k_write_data(spi->master, word_len, *tx++);
  175. if (xfer->rx_buf != NULL)
  176. *rx++ = spi100k_read_data(spi->master, word_len);
  177. } while (c);
  178. } else if (word_len <= 16) {
  179. u16 *rx;
  180. const u16 *tx;
  181. rx = xfer->rx_buf;
  182. tx = xfer->tx_buf;
  183. do {
  184. c -= 2;
  185. if (xfer->tx_buf != NULL)
  186. spi100k_write_data(spi->master, word_len, *tx++);
  187. if (xfer->rx_buf != NULL)
  188. *rx++ = spi100k_read_data(spi->master, word_len);
  189. } while (c);
  190. } else if (word_len <= 32) {
  191. u32 *rx;
  192. const u32 *tx;
  193. rx = xfer->rx_buf;
  194. tx = xfer->tx_buf;
  195. do {
  196. c -= 4;
  197. if (xfer->tx_buf != NULL)
  198. spi100k_write_data(spi->master, word_len, *tx);
  199. if (xfer->rx_buf != NULL)
  200. *rx = spi100k_read_data(spi->master, word_len);
  201. } while (c);
  202. }
  203. return count - c;
  204. }
  205. /* called only when no transfer is active to this device */
  206. static int omap1_spi100k_setup_transfer(struct spi_device *spi,
  207. struct spi_transfer *t)
  208. {
  209. struct omap1_spi100k *spi100k = spi_master_get_devdata(spi->master);
  210. struct omap1_spi100k_cs *cs = spi->controller_state;
  211. u8 word_len = spi->bits_per_word;
  212. if (t != NULL && t->bits_per_word)
  213. word_len = t->bits_per_word;
  214. if (!word_len)
  215. word_len = 8;
  216. if (spi->bits_per_word > 32)
  217. return -EINVAL;
  218. cs->word_len = word_len;
  219. /* SPI init before transfer */
  220. writew(0x3e , spi100k->base + SPI_SETUP1);
  221. writew(0x00 , spi100k->base + SPI_STATUS);
  222. writew(0x3e , spi100k->base + SPI_CTRL);
  223. return 0;
  224. }
  225. /* the spi->mode bits understood by this driver: */
  226. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
  227. static int omap1_spi100k_setup(struct spi_device *spi)
  228. {
  229. int ret;
  230. struct omap1_spi100k *spi100k;
  231. struct omap1_spi100k_cs *cs = spi->controller_state;
  232. spi100k = spi_master_get_devdata(spi->master);
  233. if (!cs) {
  234. cs = devm_kzalloc(&spi->dev, sizeof(*cs), GFP_KERNEL);
  235. if (!cs)
  236. return -ENOMEM;
  237. cs->base = spi100k->base + spi->chip_select * 0x14;
  238. spi->controller_state = cs;
  239. }
  240. spi100k_open(spi->master);
  241. clk_prepare_enable(spi100k->ick);
  242. clk_prepare_enable(spi100k->fck);
  243. ret = omap1_spi100k_setup_transfer(spi, NULL);
  244. clk_disable_unprepare(spi100k->ick);
  245. clk_disable_unprepare(spi100k->fck);
  246. return ret;
  247. }
  248. static int omap1_spi100k_prepare_hardware(struct spi_master *master)
  249. {
  250. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  251. clk_prepare_enable(spi100k->ick);
  252. clk_prepare_enable(spi100k->fck);
  253. return 0;
  254. }
  255. static int omap1_spi100k_transfer_one_message(struct spi_master *master,
  256. struct spi_message *m)
  257. {
  258. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  259. struct spi_device *spi = m->spi;
  260. struct spi_transfer *t = NULL;
  261. int cs_active = 0;
  262. int par_override = 0;
  263. int status = 0;
  264. list_for_each_entry(t, &m->transfers, transfer_list) {
  265. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  266. status = -EINVAL;
  267. break;
  268. }
  269. if (par_override || t->speed_hz || t->bits_per_word) {
  270. par_override = 1;
  271. status = omap1_spi100k_setup_transfer(spi, t);
  272. if (status < 0)
  273. break;
  274. if (!t->speed_hz && !t->bits_per_word)
  275. par_override = 0;
  276. }
  277. if (!cs_active) {
  278. omap1_spi100k_force_cs(spi100k, 1);
  279. cs_active = 1;
  280. }
  281. if (t->len) {
  282. unsigned count;
  283. count = omap1_spi100k_txrx_pio(spi, t);
  284. m->actual_length += count;
  285. if (count != t->len) {
  286. status = -EIO;
  287. break;
  288. }
  289. }
  290. if (t->delay_usecs)
  291. udelay(t->delay_usecs);
  292. /* ignore the "leave it on after last xfer" hint */
  293. if (t->cs_change) {
  294. omap1_spi100k_force_cs(spi100k, 0);
  295. cs_active = 0;
  296. }
  297. }
  298. /* Restore defaults if they were overriden */
  299. if (par_override) {
  300. par_override = 0;
  301. status = omap1_spi100k_setup_transfer(spi, NULL);
  302. }
  303. if (cs_active)
  304. omap1_spi100k_force_cs(spi100k, 0);
  305. m->status = status;
  306. spi_finalize_current_message(master);
  307. return status;
  308. }
  309. static int omap1_spi100k_unprepare_hardware(struct spi_master *master)
  310. {
  311. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  312. clk_disable_unprepare(spi100k->ick);
  313. clk_disable_unprepare(spi100k->fck);
  314. return 0;
  315. }
  316. static int omap1_spi100k_probe(struct platform_device *pdev)
  317. {
  318. struct spi_master *master;
  319. struct omap1_spi100k *spi100k;
  320. int status = 0;
  321. if (!pdev->id)
  322. return -EINVAL;
  323. master = spi_alloc_master(&pdev->dev, sizeof(*spi100k));
  324. if (master == NULL) {
  325. dev_dbg(&pdev->dev, "master allocation failed\n");
  326. return -ENOMEM;
  327. }
  328. if (pdev->id != -1)
  329. master->bus_num = pdev->id;
  330. master->setup = omap1_spi100k_setup;
  331. master->transfer_one_message = omap1_spi100k_transfer_one_message;
  332. master->prepare_transfer_hardware = omap1_spi100k_prepare_hardware;
  333. master->unprepare_transfer_hardware = omap1_spi100k_unprepare_hardware;
  334. master->cleanup = NULL;
  335. master->num_chipselect = 2;
  336. master->mode_bits = MODEBITS;
  337. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  338. master->min_speed_hz = OMAP1_SPI100K_MAX_FREQ/(1<<16);
  339. master->max_speed_hz = OMAP1_SPI100K_MAX_FREQ;
  340. spi100k = spi_master_get_devdata(master);
  341. /*
  342. * The memory region base address is taken as the platform_data.
  343. * You should allocate this with ioremap() before initializing
  344. * the SPI.
  345. */
  346. spi100k->base = (void __iomem *)dev_get_platdata(&pdev->dev);
  347. spi100k->ick = devm_clk_get(&pdev->dev, "ick");
  348. if (IS_ERR(spi100k->ick)) {
  349. dev_dbg(&pdev->dev, "can't get spi100k_ick\n");
  350. status = PTR_ERR(spi100k->ick);
  351. goto err;
  352. }
  353. spi100k->fck = devm_clk_get(&pdev->dev, "fck");
  354. if (IS_ERR(spi100k->fck)) {
  355. dev_dbg(&pdev->dev, "can't get spi100k_fck\n");
  356. status = PTR_ERR(spi100k->fck);
  357. goto err;
  358. }
  359. status = devm_spi_register_master(&pdev->dev, master);
  360. if (status < 0)
  361. goto err;
  362. return status;
  363. err:
  364. spi_master_put(master);
  365. return status;
  366. }
  367. static struct platform_driver omap1_spi100k_driver = {
  368. .driver = {
  369. .name = "omap1_spi100k",
  370. .owner = THIS_MODULE,
  371. },
  372. .probe = omap1_spi100k_probe,
  373. };
  374. module_platform_driver(omap1_spi100k_driver);
  375. MODULE_DESCRIPTION("OMAP7xx SPI 100k controller driver");
  376. MODULE_AUTHOR("Fabrice Crohas <fcrohas@gmail.com>");
  377. MODULE_LICENSE("GPL");