omap2_mcspi.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. * OMAP2 McSPI controller driver
  3. *
  4. * Copyright (C) 2005, 2006 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  6. * Juha Yrjölä <juha.yrjola@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <linux/delay.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/err.h>
  32. #include <linux/clk.h>
  33. #include <linux/io.h>
  34. #include <linux/spi/spi.h>
  35. #include <mach/dma.h>
  36. #include <mach/clock.h>
  37. #define OMAP2_MCSPI_MAX_FREQ 48000000
  38. #define OMAP2_MCSPI_REVISION 0x00
  39. #define OMAP2_MCSPI_SYSCONFIG 0x10
  40. #define OMAP2_MCSPI_SYSSTATUS 0x14
  41. #define OMAP2_MCSPI_IRQSTATUS 0x18
  42. #define OMAP2_MCSPI_IRQENABLE 0x1c
  43. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  44. #define OMAP2_MCSPI_SYST 0x24
  45. #define OMAP2_MCSPI_MODULCTRL 0x28
  46. /* per-channel banks, 0x14 bytes each, first is: */
  47. #define OMAP2_MCSPI_CHCONF0 0x2c
  48. #define OMAP2_MCSPI_CHSTAT0 0x30
  49. #define OMAP2_MCSPI_CHCTRL0 0x34
  50. #define OMAP2_MCSPI_TX0 0x38
  51. #define OMAP2_MCSPI_RX0 0x3c
  52. /* per-register bitmasks: */
  53. #define OMAP2_MCSPI_SYSCONFIG_AUTOIDLE (1 << 0)
  54. #define OMAP2_MCSPI_SYSCONFIG_SOFTRESET (1 << 1)
  55. #define OMAP2_MCSPI_SYSSTATUS_RESETDONE (1 << 0)
  56. #define OMAP2_MCSPI_MODULCTRL_SINGLE (1 << 0)
  57. #define OMAP2_MCSPI_MODULCTRL_MS (1 << 2)
  58. #define OMAP2_MCSPI_MODULCTRL_STEST (1 << 3)
  59. #define OMAP2_MCSPI_CHCONF_PHA (1 << 0)
  60. #define OMAP2_MCSPI_CHCONF_POL (1 << 1)
  61. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  62. #define OMAP2_MCSPI_CHCONF_EPOL (1 << 6)
  63. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  64. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY (0x01 << 12)
  65. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY (0x02 << 12)
  66. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  67. #define OMAP2_MCSPI_CHCONF_DMAW (1 << 14)
  68. #define OMAP2_MCSPI_CHCONF_DMAR (1 << 15)
  69. #define OMAP2_MCSPI_CHCONF_DPE0 (1 << 16)
  70. #define OMAP2_MCSPI_CHCONF_DPE1 (1 << 17)
  71. #define OMAP2_MCSPI_CHCONF_IS (1 << 18)
  72. #define OMAP2_MCSPI_CHCONF_TURBO (1 << 19)
  73. #define OMAP2_MCSPI_CHCONF_FORCE (1 << 20)
  74. #define OMAP2_MCSPI_CHSTAT_RXS (1 << 0)
  75. #define OMAP2_MCSPI_CHSTAT_TXS (1 << 1)
  76. #define OMAP2_MCSPI_CHSTAT_EOT (1 << 2)
  77. #define OMAP2_MCSPI_CHCTRL_EN (1 << 0)
  78. /* We have 2 DMA channels per CS, one for RX and one for TX */
  79. struct omap2_mcspi_dma {
  80. int dma_tx_channel;
  81. int dma_rx_channel;
  82. int dma_tx_sync_dev;
  83. int dma_rx_sync_dev;
  84. struct completion dma_tx_completion;
  85. struct completion dma_rx_completion;
  86. };
  87. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  88. * cache operations; better heuristics consider wordsize and bitrate.
  89. */
  90. #define DMA_MIN_BYTES 8
  91. struct omap2_mcspi {
  92. struct work_struct work;
  93. /* lock protects queue and registers */
  94. spinlock_t lock;
  95. struct list_head msg_queue;
  96. struct spi_master *master;
  97. struct clk *ick;
  98. struct clk *fck;
  99. /* Virtual base address of the controller */
  100. void __iomem *base;
  101. unsigned long phys;
  102. /* SPI1 has 4 channels, while SPI2 has 2 */
  103. struct omap2_mcspi_dma *dma_channels;
  104. };
  105. struct omap2_mcspi_cs {
  106. void __iomem *base;
  107. unsigned long phys;
  108. int word_len;
  109. };
  110. static struct workqueue_struct *omap2_mcspi_wq;
  111. #define MOD_REG_BIT(val, mask, set) do { \
  112. if (set) \
  113. val |= mask; \
  114. else \
  115. val &= ~mask; \
  116. } while (0)
  117. static inline void mcspi_write_reg(struct spi_master *master,
  118. int idx, u32 val)
  119. {
  120. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  121. __raw_writel(val, mcspi->base + idx);
  122. }
  123. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  124. {
  125. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  126. return __raw_readl(mcspi->base + idx);
  127. }
  128. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  129. int idx, u32 val)
  130. {
  131. struct omap2_mcspi_cs *cs = spi->controller_state;
  132. __raw_writel(val, cs->base + idx);
  133. }
  134. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  135. {
  136. struct omap2_mcspi_cs *cs = spi->controller_state;
  137. return __raw_readl(cs->base + idx);
  138. }
  139. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  140. int is_read, int enable)
  141. {
  142. u32 l, rw;
  143. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  144. if (is_read) /* 1 is read, 0 write */
  145. rw = OMAP2_MCSPI_CHCONF_DMAR;
  146. else
  147. rw = OMAP2_MCSPI_CHCONF_DMAW;
  148. MOD_REG_BIT(l, rw, enable);
  149. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
  150. }
  151. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  152. {
  153. u32 l;
  154. l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
  155. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
  156. }
  157. static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
  158. {
  159. u32 l;
  160. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  161. MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
  162. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
  163. }
  164. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  165. {
  166. u32 l;
  167. /* setup when switching from (reset default) slave mode
  168. * to single-channel master mode
  169. */
  170. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  171. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
  172. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
  173. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
  174. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  175. }
  176. static unsigned
  177. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  178. {
  179. struct omap2_mcspi *mcspi;
  180. struct omap2_mcspi_cs *cs = spi->controller_state;
  181. struct omap2_mcspi_dma *mcspi_dma;
  182. unsigned int count, c;
  183. unsigned long base, tx_reg, rx_reg;
  184. int word_len, data_type, element_count;
  185. u8 * rx;
  186. const u8 * tx;
  187. mcspi = spi_master_get_devdata(spi->master);
  188. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  189. count = xfer->len;
  190. c = count;
  191. word_len = cs->word_len;
  192. base = cs->phys;
  193. tx_reg = base + OMAP2_MCSPI_TX0;
  194. rx_reg = base + OMAP2_MCSPI_RX0;
  195. rx = xfer->rx_buf;
  196. tx = xfer->tx_buf;
  197. if (word_len <= 8) {
  198. data_type = OMAP_DMA_DATA_TYPE_S8;
  199. element_count = count;
  200. } else if (word_len <= 16) {
  201. data_type = OMAP_DMA_DATA_TYPE_S16;
  202. element_count = count >> 1;
  203. } else /* word_len <= 32 */ {
  204. data_type = OMAP_DMA_DATA_TYPE_S32;
  205. element_count = count >> 2;
  206. }
  207. if (tx != NULL) {
  208. omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
  209. data_type, element_count, 1,
  210. OMAP_DMA_SYNC_ELEMENT,
  211. mcspi_dma->dma_tx_sync_dev, 0);
  212. omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
  213. OMAP_DMA_AMODE_CONSTANT,
  214. tx_reg, 0, 0);
  215. omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
  216. OMAP_DMA_AMODE_POST_INC,
  217. xfer->tx_dma, 0, 0);
  218. }
  219. if (rx != NULL) {
  220. omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
  221. data_type, element_count, 1,
  222. OMAP_DMA_SYNC_ELEMENT,
  223. mcspi_dma->dma_rx_sync_dev, 1);
  224. omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
  225. OMAP_DMA_AMODE_CONSTANT,
  226. rx_reg, 0, 0);
  227. omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
  228. OMAP_DMA_AMODE_POST_INC,
  229. xfer->rx_dma, 0, 0);
  230. }
  231. if (tx != NULL) {
  232. omap_start_dma(mcspi_dma->dma_tx_channel);
  233. omap2_mcspi_set_dma_req(spi, 0, 1);
  234. }
  235. if (rx != NULL) {
  236. omap_start_dma(mcspi_dma->dma_rx_channel);
  237. omap2_mcspi_set_dma_req(spi, 1, 1);
  238. }
  239. if (tx != NULL) {
  240. wait_for_completion(&mcspi_dma->dma_tx_completion);
  241. dma_unmap_single(NULL, xfer->tx_dma, count, DMA_TO_DEVICE);
  242. }
  243. if (rx != NULL) {
  244. wait_for_completion(&mcspi_dma->dma_rx_completion);
  245. dma_unmap_single(NULL, xfer->rx_dma, count, DMA_FROM_DEVICE);
  246. }
  247. return count;
  248. }
  249. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  250. {
  251. unsigned long timeout;
  252. timeout = jiffies + msecs_to_jiffies(1000);
  253. while (!(__raw_readl(reg) & bit)) {
  254. if (time_after(jiffies, timeout))
  255. return -1;
  256. cpu_relax();
  257. }
  258. return 0;
  259. }
  260. static unsigned
  261. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  262. {
  263. struct omap2_mcspi *mcspi;
  264. struct omap2_mcspi_cs *cs = spi->controller_state;
  265. unsigned int count, c;
  266. u32 l;
  267. void __iomem *base = cs->base;
  268. void __iomem *tx_reg;
  269. void __iomem *rx_reg;
  270. void __iomem *chstat_reg;
  271. int word_len;
  272. mcspi = spi_master_get_devdata(spi->master);
  273. count = xfer->len;
  274. c = count;
  275. word_len = cs->word_len;
  276. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  277. l &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  278. /* We store the pre-calculated register addresses on stack to speed
  279. * up the transfer loop. */
  280. tx_reg = base + OMAP2_MCSPI_TX0;
  281. rx_reg = base + OMAP2_MCSPI_RX0;
  282. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  283. if (word_len <= 8) {
  284. u8 *rx;
  285. const u8 *tx;
  286. rx = xfer->rx_buf;
  287. tx = xfer->tx_buf;
  288. do {
  289. c -= 1;
  290. if (tx != NULL) {
  291. if (mcspi_wait_for_reg_bit(chstat_reg,
  292. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  293. dev_err(&spi->dev, "TXS timed out\n");
  294. goto out;
  295. }
  296. #ifdef VERBOSE
  297. dev_dbg(&spi->dev, "write-%d %02x\n",
  298. word_len, *tx);
  299. #endif
  300. __raw_writel(*tx++, tx_reg);
  301. }
  302. if (rx != NULL) {
  303. if (mcspi_wait_for_reg_bit(chstat_reg,
  304. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  305. dev_err(&spi->dev, "RXS timed out\n");
  306. goto out;
  307. }
  308. /* prevent last RX_ONLY read from triggering
  309. * more word i/o: switch to rx+tx
  310. */
  311. if (c == 0 && tx == NULL)
  312. mcspi_write_cs_reg(spi,
  313. OMAP2_MCSPI_CHCONF0, l);
  314. *rx++ = __raw_readl(rx_reg);
  315. #ifdef VERBOSE
  316. dev_dbg(&spi->dev, "read-%d %02x\n",
  317. word_len, *(rx - 1));
  318. #endif
  319. }
  320. } while (c);
  321. } else if (word_len <= 16) {
  322. u16 *rx;
  323. const u16 *tx;
  324. rx = xfer->rx_buf;
  325. tx = xfer->tx_buf;
  326. do {
  327. c -= 2;
  328. if (tx != NULL) {
  329. if (mcspi_wait_for_reg_bit(chstat_reg,
  330. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  331. dev_err(&spi->dev, "TXS timed out\n");
  332. goto out;
  333. }
  334. #ifdef VERBOSE
  335. dev_dbg(&spi->dev, "write-%d %04x\n",
  336. word_len, *tx);
  337. #endif
  338. __raw_writel(*tx++, tx_reg);
  339. }
  340. if (rx != NULL) {
  341. if (mcspi_wait_for_reg_bit(chstat_reg,
  342. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  343. dev_err(&spi->dev, "RXS timed out\n");
  344. goto out;
  345. }
  346. /* prevent last RX_ONLY read from triggering
  347. * more word i/o: switch to rx+tx
  348. */
  349. if (c == 0 && tx == NULL)
  350. mcspi_write_cs_reg(spi,
  351. OMAP2_MCSPI_CHCONF0, l);
  352. *rx++ = __raw_readl(rx_reg);
  353. #ifdef VERBOSE
  354. dev_dbg(&spi->dev, "read-%d %04x\n",
  355. word_len, *(rx - 1));
  356. #endif
  357. }
  358. } while (c);
  359. } else if (word_len <= 32) {
  360. u32 *rx;
  361. const u32 *tx;
  362. rx = xfer->rx_buf;
  363. tx = xfer->tx_buf;
  364. do {
  365. c -= 4;
  366. if (tx != NULL) {
  367. if (mcspi_wait_for_reg_bit(chstat_reg,
  368. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  369. dev_err(&spi->dev, "TXS timed out\n");
  370. goto out;
  371. }
  372. #ifdef VERBOSE
  373. dev_dbg(&spi->dev, "write-%d %04x\n",
  374. word_len, *tx);
  375. #endif
  376. __raw_writel(*tx++, tx_reg);
  377. }
  378. if (rx != NULL) {
  379. if (mcspi_wait_for_reg_bit(chstat_reg,
  380. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  381. dev_err(&spi->dev, "RXS timed out\n");
  382. goto out;
  383. }
  384. /* prevent last RX_ONLY read from triggering
  385. * more word i/o: switch to rx+tx
  386. */
  387. if (c == 0 && tx == NULL)
  388. mcspi_write_cs_reg(spi,
  389. OMAP2_MCSPI_CHCONF0, l);
  390. *rx++ = __raw_readl(rx_reg);
  391. #ifdef VERBOSE
  392. dev_dbg(&spi->dev, "read-%d %04x\n",
  393. word_len, *(rx - 1));
  394. #endif
  395. }
  396. } while (c);
  397. }
  398. /* for TX_ONLY mode, be sure all words have shifted out */
  399. if (xfer->rx_buf == NULL) {
  400. if (mcspi_wait_for_reg_bit(chstat_reg,
  401. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  402. dev_err(&spi->dev, "TXS timed out\n");
  403. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  404. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  405. dev_err(&spi->dev, "EOT timed out\n");
  406. }
  407. out:
  408. return count - c;
  409. }
  410. /* called only when no transfer is active to this device */
  411. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  412. struct spi_transfer *t)
  413. {
  414. struct omap2_mcspi_cs *cs = spi->controller_state;
  415. struct omap2_mcspi *mcspi;
  416. u32 l = 0, div = 0;
  417. u8 word_len = spi->bits_per_word;
  418. mcspi = spi_master_get_devdata(spi->master);
  419. if (t != NULL && t->bits_per_word)
  420. word_len = t->bits_per_word;
  421. cs->word_len = word_len;
  422. if (spi->max_speed_hz) {
  423. while (div <= 15 && (OMAP2_MCSPI_MAX_FREQ / (1 << div))
  424. > spi->max_speed_hz)
  425. div++;
  426. } else
  427. div = 15;
  428. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  429. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  430. * REVISIT: this controller could support SPI_3WIRE mode.
  431. */
  432. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  433. l |= OMAP2_MCSPI_CHCONF_DPE0;
  434. /* wordlength */
  435. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  436. l |= (word_len - 1) << 7;
  437. /* set chipselect polarity; manage with FORCE */
  438. if (!(spi->mode & SPI_CS_HIGH))
  439. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  440. else
  441. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  442. /* set clock divisor */
  443. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  444. l |= div << 2;
  445. /* set SPI mode 0..3 */
  446. if (spi->mode & SPI_CPOL)
  447. l |= OMAP2_MCSPI_CHCONF_POL;
  448. else
  449. l &= ~OMAP2_MCSPI_CHCONF_POL;
  450. if (spi->mode & SPI_CPHA)
  451. l |= OMAP2_MCSPI_CHCONF_PHA;
  452. else
  453. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  454. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
  455. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  456. OMAP2_MCSPI_MAX_FREQ / (1 << div),
  457. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  458. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  459. return 0;
  460. }
  461. static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
  462. {
  463. struct spi_device *spi = data;
  464. struct omap2_mcspi *mcspi;
  465. struct omap2_mcspi_dma *mcspi_dma;
  466. mcspi = spi_master_get_devdata(spi->master);
  467. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  468. complete(&mcspi_dma->dma_rx_completion);
  469. /* We must disable the DMA RX request */
  470. omap2_mcspi_set_dma_req(spi, 1, 0);
  471. }
  472. static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
  473. {
  474. struct spi_device *spi = data;
  475. struct omap2_mcspi *mcspi;
  476. struct omap2_mcspi_dma *mcspi_dma;
  477. mcspi = spi_master_get_devdata(spi->master);
  478. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  479. complete(&mcspi_dma->dma_tx_completion);
  480. /* We must disable the DMA TX request */
  481. omap2_mcspi_set_dma_req(spi, 0, 0);
  482. }
  483. static int omap2_mcspi_request_dma(struct spi_device *spi)
  484. {
  485. struct spi_master *master = spi->master;
  486. struct omap2_mcspi *mcspi;
  487. struct omap2_mcspi_dma *mcspi_dma;
  488. mcspi = spi_master_get_devdata(master);
  489. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  490. if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
  491. omap2_mcspi_dma_rx_callback, spi,
  492. &mcspi_dma->dma_rx_channel)) {
  493. dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
  494. return -EAGAIN;
  495. }
  496. if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
  497. omap2_mcspi_dma_tx_callback, spi,
  498. &mcspi_dma->dma_tx_channel)) {
  499. omap_free_dma(mcspi_dma->dma_rx_channel);
  500. mcspi_dma->dma_rx_channel = -1;
  501. dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
  502. return -EAGAIN;
  503. }
  504. init_completion(&mcspi_dma->dma_rx_completion);
  505. init_completion(&mcspi_dma->dma_tx_completion);
  506. return 0;
  507. }
  508. static int omap2_mcspi_setup(struct spi_device *spi)
  509. {
  510. int ret;
  511. struct omap2_mcspi *mcspi;
  512. struct omap2_mcspi_dma *mcspi_dma;
  513. struct omap2_mcspi_cs *cs = spi->controller_state;
  514. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  515. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  516. spi->bits_per_word);
  517. return -EINVAL;
  518. }
  519. mcspi = spi_master_get_devdata(spi->master);
  520. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  521. if (!cs) {
  522. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  523. if (!cs)
  524. return -ENOMEM;
  525. cs->base = mcspi->base + spi->chip_select * 0x14;
  526. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  527. spi->controller_state = cs;
  528. }
  529. if (mcspi_dma->dma_rx_channel == -1
  530. || mcspi_dma->dma_tx_channel == -1) {
  531. ret = omap2_mcspi_request_dma(spi);
  532. if (ret < 0)
  533. return ret;
  534. }
  535. clk_enable(mcspi->ick);
  536. clk_enable(mcspi->fck);
  537. ret = omap2_mcspi_setup_transfer(spi, NULL);
  538. clk_disable(mcspi->fck);
  539. clk_disable(mcspi->ick);
  540. return ret;
  541. }
  542. static void omap2_mcspi_cleanup(struct spi_device *spi)
  543. {
  544. struct omap2_mcspi *mcspi;
  545. struct omap2_mcspi_dma *mcspi_dma;
  546. mcspi = spi_master_get_devdata(spi->master);
  547. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  548. kfree(spi->controller_state);
  549. if (mcspi_dma->dma_rx_channel != -1) {
  550. omap_free_dma(mcspi_dma->dma_rx_channel);
  551. mcspi_dma->dma_rx_channel = -1;
  552. }
  553. if (mcspi_dma->dma_tx_channel != -1) {
  554. omap_free_dma(mcspi_dma->dma_tx_channel);
  555. mcspi_dma->dma_tx_channel = -1;
  556. }
  557. }
  558. static void omap2_mcspi_work(struct work_struct *work)
  559. {
  560. struct omap2_mcspi *mcspi;
  561. mcspi = container_of(work, struct omap2_mcspi, work);
  562. spin_lock_irq(&mcspi->lock);
  563. clk_enable(mcspi->ick);
  564. clk_enable(mcspi->fck);
  565. /* We only enable one channel at a time -- the one whose message is
  566. * at the head of the queue -- although this controller would gladly
  567. * arbitrate among multiple channels. This corresponds to "single
  568. * channel" master mode. As a side effect, we need to manage the
  569. * chipselect with the FORCE bit ... CS != channel enable.
  570. */
  571. while (!list_empty(&mcspi->msg_queue)) {
  572. struct spi_message *m;
  573. struct spi_device *spi;
  574. struct spi_transfer *t = NULL;
  575. int cs_active = 0;
  576. struct omap2_mcspi_cs *cs;
  577. int par_override = 0;
  578. int status = 0;
  579. u32 chconf;
  580. m = container_of(mcspi->msg_queue.next, struct spi_message,
  581. queue);
  582. list_del_init(&m->queue);
  583. spin_unlock_irq(&mcspi->lock);
  584. spi = m->spi;
  585. cs = spi->controller_state;
  586. omap2_mcspi_set_enable(spi, 1);
  587. list_for_each_entry(t, &m->transfers, transfer_list) {
  588. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  589. status = -EINVAL;
  590. break;
  591. }
  592. if (par_override || t->speed_hz || t->bits_per_word) {
  593. par_override = 1;
  594. status = omap2_mcspi_setup_transfer(spi, t);
  595. if (status < 0)
  596. break;
  597. if (!t->speed_hz && !t->bits_per_word)
  598. par_override = 0;
  599. }
  600. if (!cs_active) {
  601. omap2_mcspi_force_cs(spi, 1);
  602. cs_active = 1;
  603. }
  604. chconf = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  605. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  606. if (t->tx_buf == NULL)
  607. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  608. else if (t->rx_buf == NULL)
  609. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  610. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, chconf);
  611. if (t->len) {
  612. unsigned count;
  613. /* RX_ONLY mode needs dummy data in TX reg */
  614. if (t->tx_buf == NULL)
  615. __raw_writel(0, cs->base
  616. + OMAP2_MCSPI_TX0);
  617. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  618. count = omap2_mcspi_txrx_dma(spi, t);
  619. else
  620. count = omap2_mcspi_txrx_pio(spi, t);
  621. m->actual_length += count;
  622. if (count != t->len) {
  623. status = -EIO;
  624. break;
  625. }
  626. }
  627. if (t->delay_usecs)
  628. udelay(t->delay_usecs);
  629. /* ignore the "leave it on after last xfer" hint */
  630. if (t->cs_change) {
  631. omap2_mcspi_force_cs(spi, 0);
  632. cs_active = 0;
  633. }
  634. }
  635. /* Restore defaults if they were overriden */
  636. if (par_override) {
  637. par_override = 0;
  638. status = omap2_mcspi_setup_transfer(spi, NULL);
  639. }
  640. if (cs_active)
  641. omap2_mcspi_force_cs(spi, 0);
  642. omap2_mcspi_set_enable(spi, 0);
  643. m->status = status;
  644. m->complete(m->context);
  645. spin_lock_irq(&mcspi->lock);
  646. }
  647. clk_disable(mcspi->fck);
  648. clk_disable(mcspi->ick);
  649. spin_unlock_irq(&mcspi->lock);
  650. }
  651. static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
  652. {
  653. struct omap2_mcspi *mcspi;
  654. unsigned long flags;
  655. struct spi_transfer *t;
  656. m->actual_length = 0;
  657. m->status = 0;
  658. /* reject invalid messages and transfers */
  659. if (list_empty(&m->transfers) || !m->complete)
  660. return -EINVAL;
  661. list_for_each_entry(t, &m->transfers, transfer_list) {
  662. const void *tx_buf = t->tx_buf;
  663. void *rx_buf = t->rx_buf;
  664. unsigned len = t->len;
  665. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  666. || (len && !(rx_buf || tx_buf))
  667. || (t->bits_per_word &&
  668. ( t->bits_per_word < 4
  669. || t->bits_per_word > 32))) {
  670. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  671. t->speed_hz,
  672. len,
  673. tx_buf ? "tx" : "",
  674. rx_buf ? "rx" : "",
  675. t->bits_per_word);
  676. return -EINVAL;
  677. }
  678. if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
  679. dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
  680. t->speed_hz,
  681. OMAP2_MCSPI_MAX_FREQ/(1<<16));
  682. return -EINVAL;
  683. }
  684. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  685. continue;
  686. /* Do DMA mapping "early" for better error reporting and
  687. * dcache use. Note that if dma_unmap_single() ever starts
  688. * to do real work on ARM, we'd need to clean up mappings
  689. * for previous transfers on *ALL* exits of this loop...
  690. */
  691. if (tx_buf != NULL) {
  692. t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
  693. len, DMA_TO_DEVICE);
  694. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  695. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  696. 'T', len);
  697. return -EINVAL;
  698. }
  699. }
  700. if (rx_buf != NULL) {
  701. t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
  702. DMA_FROM_DEVICE);
  703. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  704. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  705. 'R', len);
  706. if (tx_buf != NULL)
  707. dma_unmap_single(NULL, t->tx_dma,
  708. len, DMA_TO_DEVICE);
  709. return -EINVAL;
  710. }
  711. }
  712. }
  713. mcspi = spi_master_get_devdata(spi->master);
  714. spin_lock_irqsave(&mcspi->lock, flags);
  715. list_add_tail(&m->queue, &mcspi->msg_queue);
  716. queue_work(omap2_mcspi_wq, &mcspi->work);
  717. spin_unlock_irqrestore(&mcspi->lock, flags);
  718. return 0;
  719. }
  720. static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
  721. {
  722. struct spi_master *master = mcspi->master;
  723. u32 tmp;
  724. clk_enable(mcspi->ick);
  725. clk_enable(mcspi->fck);
  726. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  727. OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
  728. do {
  729. tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
  730. } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
  731. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  732. /* (3 << 8) | (2 << 3) | */
  733. OMAP2_MCSPI_SYSCONFIG_AUTOIDLE);
  734. omap2_mcspi_set_master_mode(master);
  735. clk_disable(mcspi->fck);
  736. clk_disable(mcspi->ick);
  737. return 0;
  738. }
  739. static u8 __initdata spi1_rxdma_id [] = {
  740. OMAP24XX_DMA_SPI1_RX0,
  741. OMAP24XX_DMA_SPI1_RX1,
  742. OMAP24XX_DMA_SPI1_RX2,
  743. OMAP24XX_DMA_SPI1_RX3,
  744. };
  745. static u8 __initdata spi1_txdma_id [] = {
  746. OMAP24XX_DMA_SPI1_TX0,
  747. OMAP24XX_DMA_SPI1_TX1,
  748. OMAP24XX_DMA_SPI1_TX2,
  749. OMAP24XX_DMA_SPI1_TX3,
  750. };
  751. static u8 __initdata spi2_rxdma_id[] = {
  752. OMAP24XX_DMA_SPI2_RX0,
  753. OMAP24XX_DMA_SPI2_RX1,
  754. };
  755. static u8 __initdata spi2_txdma_id[] = {
  756. OMAP24XX_DMA_SPI2_TX0,
  757. OMAP24XX_DMA_SPI2_TX1,
  758. };
  759. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP34XX)
  760. static u8 __initdata spi3_rxdma_id[] = {
  761. OMAP24XX_DMA_SPI3_RX0,
  762. OMAP24XX_DMA_SPI3_RX1,
  763. };
  764. static u8 __initdata spi3_txdma_id[] = {
  765. OMAP24XX_DMA_SPI3_TX0,
  766. OMAP24XX_DMA_SPI3_TX1,
  767. };
  768. #endif
  769. #ifdef CONFIG_ARCH_OMAP3
  770. static u8 __initdata spi4_rxdma_id[] = {
  771. OMAP34XX_DMA_SPI4_RX0,
  772. };
  773. static u8 __initdata spi4_txdma_id[] = {
  774. OMAP34XX_DMA_SPI4_TX0,
  775. };
  776. #endif
  777. static int __init omap2_mcspi_probe(struct platform_device *pdev)
  778. {
  779. struct spi_master *master;
  780. struct omap2_mcspi *mcspi;
  781. struct resource *r;
  782. int status = 0, i;
  783. const u8 *rxdma_id, *txdma_id;
  784. unsigned num_chipselect;
  785. switch (pdev->id) {
  786. case 1:
  787. rxdma_id = spi1_rxdma_id;
  788. txdma_id = spi1_txdma_id;
  789. num_chipselect = 4;
  790. break;
  791. case 2:
  792. rxdma_id = spi2_rxdma_id;
  793. txdma_id = spi2_txdma_id;
  794. num_chipselect = 2;
  795. break;
  796. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3)
  797. case 3:
  798. rxdma_id = spi3_rxdma_id;
  799. txdma_id = spi3_txdma_id;
  800. num_chipselect = 2;
  801. break;
  802. #endif
  803. #ifdef CONFIG_ARCH_OMAP3
  804. case 4:
  805. rxdma_id = spi4_rxdma_id;
  806. txdma_id = spi4_txdma_id;
  807. num_chipselect = 1;
  808. break;
  809. #endif
  810. default:
  811. return -EINVAL;
  812. }
  813. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  814. if (master == NULL) {
  815. dev_dbg(&pdev->dev, "master allocation failed\n");
  816. return -ENOMEM;
  817. }
  818. /* the spi->mode bits understood by this driver: */
  819. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  820. if (pdev->id != -1)
  821. master->bus_num = pdev->id;
  822. master->setup = omap2_mcspi_setup;
  823. master->transfer = omap2_mcspi_transfer;
  824. master->cleanup = omap2_mcspi_cleanup;
  825. master->num_chipselect = num_chipselect;
  826. dev_set_drvdata(&pdev->dev, master);
  827. mcspi = spi_master_get_devdata(master);
  828. mcspi->master = master;
  829. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  830. if (r == NULL) {
  831. status = -ENODEV;
  832. goto err1;
  833. }
  834. if (!request_mem_region(r->start, (r->end - r->start) + 1,
  835. dev_name(&pdev->dev))) {
  836. status = -EBUSY;
  837. goto err1;
  838. }
  839. mcspi->phys = r->start;
  840. mcspi->base = ioremap(r->start, r->end - r->start + 1);
  841. if (!mcspi->base) {
  842. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  843. status = -ENOMEM;
  844. goto err1aa;
  845. }
  846. INIT_WORK(&mcspi->work, omap2_mcspi_work);
  847. spin_lock_init(&mcspi->lock);
  848. INIT_LIST_HEAD(&mcspi->msg_queue);
  849. mcspi->ick = clk_get(&pdev->dev, "ick");
  850. if (IS_ERR(mcspi->ick)) {
  851. dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
  852. status = PTR_ERR(mcspi->ick);
  853. goto err1a;
  854. }
  855. mcspi->fck = clk_get(&pdev->dev, "fck");
  856. if (IS_ERR(mcspi->fck)) {
  857. dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
  858. status = PTR_ERR(mcspi->fck);
  859. goto err2;
  860. }
  861. mcspi->dma_channels = kcalloc(master->num_chipselect,
  862. sizeof(struct omap2_mcspi_dma),
  863. GFP_KERNEL);
  864. if (mcspi->dma_channels == NULL)
  865. goto err3;
  866. for (i = 0; i < num_chipselect; i++) {
  867. mcspi->dma_channels[i].dma_rx_channel = -1;
  868. mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
  869. mcspi->dma_channels[i].dma_tx_channel = -1;
  870. mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
  871. }
  872. if (omap2_mcspi_reset(mcspi) < 0)
  873. goto err4;
  874. status = spi_register_master(master);
  875. if (status < 0)
  876. goto err4;
  877. return status;
  878. err4:
  879. kfree(mcspi->dma_channels);
  880. err3:
  881. clk_put(mcspi->fck);
  882. err2:
  883. clk_put(mcspi->ick);
  884. err1a:
  885. iounmap(mcspi->base);
  886. err1aa:
  887. release_mem_region(r->start, (r->end - r->start) + 1);
  888. err1:
  889. spi_master_put(master);
  890. return status;
  891. }
  892. static int __exit omap2_mcspi_remove(struct platform_device *pdev)
  893. {
  894. struct spi_master *master;
  895. struct omap2_mcspi *mcspi;
  896. struct omap2_mcspi_dma *dma_channels;
  897. struct resource *r;
  898. void __iomem *base;
  899. master = dev_get_drvdata(&pdev->dev);
  900. mcspi = spi_master_get_devdata(master);
  901. dma_channels = mcspi->dma_channels;
  902. clk_put(mcspi->fck);
  903. clk_put(mcspi->ick);
  904. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  905. release_mem_region(r->start, (r->end - r->start) + 1);
  906. base = mcspi->base;
  907. spi_unregister_master(master);
  908. iounmap(base);
  909. kfree(dma_channels);
  910. return 0;
  911. }
  912. /* work with hotplug and coldplug */
  913. MODULE_ALIAS("platform:omap2_mcspi");
  914. static struct platform_driver omap2_mcspi_driver = {
  915. .driver = {
  916. .name = "omap2_mcspi",
  917. .owner = THIS_MODULE,
  918. },
  919. .remove = __exit_p(omap2_mcspi_remove),
  920. };
  921. static int __init omap2_mcspi_init(void)
  922. {
  923. omap2_mcspi_wq = create_singlethread_workqueue(
  924. omap2_mcspi_driver.driver.name);
  925. if (omap2_mcspi_wq == NULL)
  926. return -1;
  927. return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
  928. }
  929. subsys_initcall(omap2_mcspi_init);
  930. static void __exit omap2_mcspi_exit(void)
  931. {
  932. platform_driver_unregister(&omap2_mcspi_driver);
  933. destroy_workqueue(omap2_mcspi_wq);
  934. }
  935. module_exit(omap2_mcspi_exit);
  936. MODULE_LICENSE("GPL");