omap2_mcspi.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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/slab.h>
  35. #include <linux/spi/spi.h>
  36. #include <plat/dma.h>
  37. #include <plat/clock.h>
  38. #include <plat/mcspi.h>
  39. #define OMAP2_MCSPI_MAX_FREQ 48000000
  40. /* OMAP2 has 3 SPI controllers, while OMAP3 has 4 */
  41. #define OMAP2_MCSPI_MAX_CTRL 4
  42. #define OMAP2_MCSPI_REVISION 0x00
  43. #define OMAP2_MCSPI_SYSCONFIG 0x10
  44. #define OMAP2_MCSPI_SYSSTATUS 0x14
  45. #define OMAP2_MCSPI_IRQSTATUS 0x18
  46. #define OMAP2_MCSPI_IRQENABLE 0x1c
  47. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  48. #define OMAP2_MCSPI_SYST 0x24
  49. #define OMAP2_MCSPI_MODULCTRL 0x28
  50. /* per-channel banks, 0x14 bytes each, first is: */
  51. #define OMAP2_MCSPI_CHCONF0 0x2c
  52. #define OMAP2_MCSPI_CHSTAT0 0x30
  53. #define OMAP2_MCSPI_CHCTRL0 0x34
  54. #define OMAP2_MCSPI_TX0 0x38
  55. #define OMAP2_MCSPI_RX0 0x3c
  56. /* per-register bitmasks: */
  57. #define OMAP2_MCSPI_SYSCONFIG_SMARTIDLE BIT(4)
  58. #define OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2)
  59. #define OMAP2_MCSPI_SYSCONFIG_AUTOIDLE BIT(0)
  60. #define OMAP2_MCSPI_SYSCONFIG_SOFTRESET BIT(1)
  61. #define OMAP2_MCSPI_SYSSTATUS_RESETDONE BIT(0)
  62. #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
  63. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  64. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  65. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  66. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  67. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  68. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  69. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  70. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  71. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  72. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  73. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  74. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  75. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  76. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  77. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  78. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  79. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  80. #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
  81. #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
  82. #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
  83. #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
  84. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  85. /* We have 2 DMA channels per CS, one for RX and one for TX */
  86. struct omap2_mcspi_dma {
  87. int dma_tx_channel;
  88. int dma_rx_channel;
  89. int dma_tx_sync_dev;
  90. int dma_rx_sync_dev;
  91. struct completion dma_tx_completion;
  92. struct completion dma_rx_completion;
  93. };
  94. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  95. * cache operations; better heuristics consider wordsize and bitrate.
  96. */
  97. #define DMA_MIN_BYTES 160
  98. struct omap2_mcspi {
  99. struct work_struct work;
  100. /* lock protects queue and registers */
  101. spinlock_t lock;
  102. struct list_head msg_queue;
  103. struct spi_master *master;
  104. struct clk *ick;
  105. struct clk *fck;
  106. /* Virtual base address of the controller */
  107. void __iomem *base;
  108. unsigned long phys;
  109. /* SPI1 has 4 channels, while SPI2 has 2 */
  110. struct omap2_mcspi_dma *dma_channels;
  111. };
  112. struct omap2_mcspi_cs {
  113. void __iomem *base;
  114. unsigned long phys;
  115. int word_len;
  116. struct list_head node;
  117. /* Context save and restore shadow register */
  118. u32 chconf0;
  119. };
  120. /* used for context save and restore, structure members to be updated whenever
  121. * corresponding registers are modified.
  122. */
  123. struct omap2_mcspi_regs {
  124. u32 sysconfig;
  125. u32 modulctrl;
  126. u32 wakeupenable;
  127. struct list_head cs;
  128. };
  129. static struct omap2_mcspi_regs omap2_mcspi_ctx[OMAP2_MCSPI_MAX_CTRL];
  130. static struct workqueue_struct *omap2_mcspi_wq;
  131. #define MOD_REG_BIT(val, mask, set) do { \
  132. if (set) \
  133. val |= mask; \
  134. else \
  135. val &= ~mask; \
  136. } while (0)
  137. static inline void mcspi_write_reg(struct spi_master *master,
  138. int idx, u32 val)
  139. {
  140. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  141. __raw_writel(val, mcspi->base + idx);
  142. }
  143. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  144. {
  145. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  146. return __raw_readl(mcspi->base + idx);
  147. }
  148. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  149. int idx, u32 val)
  150. {
  151. struct omap2_mcspi_cs *cs = spi->controller_state;
  152. __raw_writel(val, cs->base + idx);
  153. }
  154. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  155. {
  156. struct omap2_mcspi_cs *cs = spi->controller_state;
  157. return __raw_readl(cs->base + idx);
  158. }
  159. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  160. {
  161. struct omap2_mcspi_cs *cs = spi->controller_state;
  162. return cs->chconf0;
  163. }
  164. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  165. {
  166. struct omap2_mcspi_cs *cs = spi->controller_state;
  167. cs->chconf0 = val;
  168. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  169. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  170. }
  171. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  172. int is_read, int enable)
  173. {
  174. u32 l, rw;
  175. l = mcspi_cached_chconf0(spi);
  176. if (is_read) /* 1 is read, 0 write */
  177. rw = OMAP2_MCSPI_CHCONF_DMAR;
  178. else
  179. rw = OMAP2_MCSPI_CHCONF_DMAW;
  180. MOD_REG_BIT(l, rw, enable);
  181. mcspi_write_chconf0(spi, l);
  182. }
  183. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  184. {
  185. u32 l;
  186. l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
  187. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
  188. /* Flash post-writes */
  189. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  190. }
  191. static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
  192. {
  193. u32 l;
  194. l = mcspi_cached_chconf0(spi);
  195. MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
  196. mcspi_write_chconf0(spi, l);
  197. }
  198. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  199. {
  200. u32 l;
  201. /* setup when switching from (reset default) slave mode
  202. * to single-channel master mode
  203. */
  204. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  205. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
  206. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
  207. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
  208. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  209. omap2_mcspi_ctx[master->bus_num - 1].modulctrl = l;
  210. }
  211. static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
  212. {
  213. struct spi_master *spi_cntrl;
  214. struct omap2_mcspi_cs *cs;
  215. spi_cntrl = mcspi->master;
  216. /* McSPI: context restore */
  217. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL,
  218. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].modulctrl);
  219. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_SYSCONFIG,
  220. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].sysconfig);
  221. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE,
  222. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].wakeupenable);
  223. list_for_each_entry(cs, &omap2_mcspi_ctx[spi_cntrl->bus_num - 1].cs,
  224. node)
  225. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  226. }
  227. static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
  228. {
  229. clk_disable(mcspi->ick);
  230. clk_disable(mcspi->fck);
  231. }
  232. static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
  233. {
  234. if (clk_enable(mcspi->ick))
  235. return -ENODEV;
  236. if (clk_enable(mcspi->fck))
  237. return -ENODEV;
  238. omap2_mcspi_restore_ctx(mcspi);
  239. return 0;
  240. }
  241. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  242. {
  243. unsigned long timeout;
  244. timeout = jiffies + msecs_to_jiffies(1000);
  245. while (!(__raw_readl(reg) & bit)) {
  246. if (time_after(jiffies, timeout))
  247. return -1;
  248. cpu_relax();
  249. }
  250. return 0;
  251. }
  252. static unsigned
  253. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  254. {
  255. struct omap2_mcspi *mcspi;
  256. struct omap2_mcspi_cs *cs = spi->controller_state;
  257. struct omap2_mcspi_dma *mcspi_dma;
  258. unsigned int count, c;
  259. unsigned long base, tx_reg, rx_reg;
  260. int word_len, data_type, element_count;
  261. int elements;
  262. u32 l;
  263. u8 * rx;
  264. const u8 * tx;
  265. void __iomem *chstat_reg;
  266. mcspi = spi_master_get_devdata(spi->master);
  267. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  268. l = mcspi_cached_chconf0(spi);
  269. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  270. count = xfer->len;
  271. c = count;
  272. word_len = cs->word_len;
  273. base = cs->phys;
  274. tx_reg = base + OMAP2_MCSPI_TX0;
  275. rx_reg = base + OMAP2_MCSPI_RX0;
  276. rx = xfer->rx_buf;
  277. tx = xfer->tx_buf;
  278. if (word_len <= 8) {
  279. data_type = OMAP_DMA_DATA_TYPE_S8;
  280. element_count = count;
  281. } else if (word_len <= 16) {
  282. data_type = OMAP_DMA_DATA_TYPE_S16;
  283. element_count = count >> 1;
  284. } else /* word_len <= 32 */ {
  285. data_type = OMAP_DMA_DATA_TYPE_S32;
  286. element_count = count >> 2;
  287. }
  288. if (tx != NULL) {
  289. omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
  290. data_type, element_count, 1,
  291. OMAP_DMA_SYNC_ELEMENT,
  292. mcspi_dma->dma_tx_sync_dev, 0);
  293. omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
  294. OMAP_DMA_AMODE_CONSTANT,
  295. tx_reg, 0, 0);
  296. omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
  297. OMAP_DMA_AMODE_POST_INC,
  298. xfer->tx_dma, 0, 0);
  299. }
  300. if (rx != NULL) {
  301. elements = element_count - 1;
  302. if (l & OMAP2_MCSPI_CHCONF_TURBO)
  303. elements--;
  304. omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
  305. data_type, elements, 1,
  306. OMAP_DMA_SYNC_ELEMENT,
  307. mcspi_dma->dma_rx_sync_dev, 1);
  308. omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
  309. OMAP_DMA_AMODE_CONSTANT,
  310. rx_reg, 0, 0);
  311. omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
  312. OMAP_DMA_AMODE_POST_INC,
  313. xfer->rx_dma, 0, 0);
  314. }
  315. if (tx != NULL) {
  316. omap_start_dma(mcspi_dma->dma_tx_channel);
  317. omap2_mcspi_set_dma_req(spi, 0, 1);
  318. }
  319. if (rx != NULL) {
  320. omap_start_dma(mcspi_dma->dma_rx_channel);
  321. omap2_mcspi_set_dma_req(spi, 1, 1);
  322. }
  323. if (tx != NULL) {
  324. wait_for_completion(&mcspi_dma->dma_tx_completion);
  325. dma_unmap_single(&spi->dev, xfer->tx_dma, count, DMA_TO_DEVICE);
  326. /* for TX_ONLY mode, be sure all words have shifted out */
  327. if (rx == NULL) {
  328. if (mcspi_wait_for_reg_bit(chstat_reg,
  329. OMAP2_MCSPI_CHSTAT_TXS) < 0)
  330. dev_err(&spi->dev, "TXS timed out\n");
  331. else if (mcspi_wait_for_reg_bit(chstat_reg,
  332. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  333. dev_err(&spi->dev, "EOT timed out\n");
  334. }
  335. }
  336. if (rx != NULL) {
  337. wait_for_completion(&mcspi_dma->dma_rx_completion);
  338. dma_unmap_single(&spi->dev, xfer->rx_dma, count, DMA_FROM_DEVICE);
  339. omap2_mcspi_set_enable(spi, 0);
  340. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  341. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  342. & OMAP2_MCSPI_CHSTAT_RXS)) {
  343. u32 w;
  344. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  345. if (word_len <= 8)
  346. ((u8 *)xfer->rx_buf)[elements++] = w;
  347. else if (word_len <= 16)
  348. ((u16 *)xfer->rx_buf)[elements++] = w;
  349. else /* word_len <= 32 */
  350. ((u32 *)xfer->rx_buf)[elements++] = w;
  351. } else {
  352. dev_err(&spi->dev,
  353. "DMA RX penultimate word empty");
  354. count -= (word_len <= 8) ? 2 :
  355. (word_len <= 16) ? 4 :
  356. /* word_len <= 32 */ 8;
  357. omap2_mcspi_set_enable(spi, 1);
  358. return count;
  359. }
  360. }
  361. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  362. & OMAP2_MCSPI_CHSTAT_RXS)) {
  363. u32 w;
  364. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  365. if (word_len <= 8)
  366. ((u8 *)xfer->rx_buf)[elements] = w;
  367. else if (word_len <= 16)
  368. ((u16 *)xfer->rx_buf)[elements] = w;
  369. else /* word_len <= 32 */
  370. ((u32 *)xfer->rx_buf)[elements] = w;
  371. } else {
  372. dev_err(&spi->dev, "DMA RX last word empty");
  373. count -= (word_len <= 8) ? 1 :
  374. (word_len <= 16) ? 2 :
  375. /* word_len <= 32 */ 4;
  376. }
  377. omap2_mcspi_set_enable(spi, 1);
  378. }
  379. return count;
  380. }
  381. static unsigned
  382. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  383. {
  384. struct omap2_mcspi *mcspi;
  385. struct omap2_mcspi_cs *cs = spi->controller_state;
  386. unsigned int count, c;
  387. u32 l;
  388. void __iomem *base = cs->base;
  389. void __iomem *tx_reg;
  390. void __iomem *rx_reg;
  391. void __iomem *chstat_reg;
  392. int word_len;
  393. mcspi = spi_master_get_devdata(spi->master);
  394. count = xfer->len;
  395. c = count;
  396. word_len = cs->word_len;
  397. l = mcspi_cached_chconf0(spi);
  398. /* We store the pre-calculated register addresses on stack to speed
  399. * up the transfer loop. */
  400. tx_reg = base + OMAP2_MCSPI_TX0;
  401. rx_reg = base + OMAP2_MCSPI_RX0;
  402. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  403. if (word_len <= 8) {
  404. u8 *rx;
  405. const u8 *tx;
  406. rx = xfer->rx_buf;
  407. tx = xfer->tx_buf;
  408. do {
  409. c -= 1;
  410. if (tx != NULL) {
  411. if (mcspi_wait_for_reg_bit(chstat_reg,
  412. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  413. dev_err(&spi->dev, "TXS timed out\n");
  414. goto out;
  415. }
  416. dev_vdbg(&spi->dev, "write-%d %02x\n",
  417. word_len, *tx);
  418. __raw_writel(*tx++, tx_reg);
  419. }
  420. if (rx != NULL) {
  421. if (mcspi_wait_for_reg_bit(chstat_reg,
  422. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  423. dev_err(&spi->dev, "RXS timed out\n");
  424. goto out;
  425. }
  426. if (c == 1 && tx == NULL &&
  427. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  428. omap2_mcspi_set_enable(spi, 0);
  429. *rx++ = __raw_readl(rx_reg);
  430. dev_vdbg(&spi->dev, "read-%d %02x\n",
  431. word_len, *(rx - 1));
  432. if (mcspi_wait_for_reg_bit(chstat_reg,
  433. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  434. dev_err(&spi->dev,
  435. "RXS timed out\n");
  436. goto out;
  437. }
  438. c = 0;
  439. } else if (c == 0 && tx == NULL) {
  440. omap2_mcspi_set_enable(spi, 0);
  441. }
  442. *rx++ = __raw_readl(rx_reg);
  443. dev_vdbg(&spi->dev, "read-%d %02x\n",
  444. word_len, *(rx - 1));
  445. }
  446. } while (c);
  447. } else if (word_len <= 16) {
  448. u16 *rx;
  449. const u16 *tx;
  450. rx = xfer->rx_buf;
  451. tx = xfer->tx_buf;
  452. do {
  453. c -= 2;
  454. if (tx != NULL) {
  455. if (mcspi_wait_for_reg_bit(chstat_reg,
  456. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  457. dev_err(&spi->dev, "TXS timed out\n");
  458. goto out;
  459. }
  460. dev_vdbg(&spi->dev, "write-%d %04x\n",
  461. word_len, *tx);
  462. __raw_writel(*tx++, tx_reg);
  463. }
  464. if (rx != NULL) {
  465. if (mcspi_wait_for_reg_bit(chstat_reg,
  466. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  467. dev_err(&spi->dev, "RXS timed out\n");
  468. goto out;
  469. }
  470. if (c == 2 && tx == NULL &&
  471. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  472. omap2_mcspi_set_enable(spi, 0);
  473. *rx++ = __raw_readl(rx_reg);
  474. dev_vdbg(&spi->dev, "read-%d %04x\n",
  475. word_len, *(rx - 1));
  476. if (mcspi_wait_for_reg_bit(chstat_reg,
  477. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  478. dev_err(&spi->dev,
  479. "RXS timed out\n");
  480. goto out;
  481. }
  482. c = 0;
  483. } else if (c == 0 && tx == NULL) {
  484. omap2_mcspi_set_enable(spi, 0);
  485. }
  486. *rx++ = __raw_readl(rx_reg);
  487. dev_vdbg(&spi->dev, "read-%d %04x\n",
  488. word_len, *(rx - 1));
  489. }
  490. } while (c);
  491. } else if (word_len <= 32) {
  492. u32 *rx;
  493. const u32 *tx;
  494. rx = xfer->rx_buf;
  495. tx = xfer->tx_buf;
  496. do {
  497. c -= 4;
  498. if (tx != NULL) {
  499. if (mcspi_wait_for_reg_bit(chstat_reg,
  500. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  501. dev_err(&spi->dev, "TXS timed out\n");
  502. goto out;
  503. }
  504. dev_vdbg(&spi->dev, "write-%d %08x\n",
  505. word_len, *tx);
  506. __raw_writel(*tx++, tx_reg);
  507. }
  508. if (rx != NULL) {
  509. if (mcspi_wait_for_reg_bit(chstat_reg,
  510. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  511. dev_err(&spi->dev, "RXS timed out\n");
  512. goto out;
  513. }
  514. if (c == 4 && tx == NULL &&
  515. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  516. omap2_mcspi_set_enable(spi, 0);
  517. *rx++ = __raw_readl(rx_reg);
  518. dev_vdbg(&spi->dev, "read-%d %08x\n",
  519. word_len, *(rx - 1));
  520. if (mcspi_wait_for_reg_bit(chstat_reg,
  521. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  522. dev_err(&spi->dev,
  523. "RXS timed out\n");
  524. goto out;
  525. }
  526. c = 0;
  527. } else if (c == 0 && tx == NULL) {
  528. omap2_mcspi_set_enable(spi, 0);
  529. }
  530. *rx++ = __raw_readl(rx_reg);
  531. dev_vdbg(&spi->dev, "read-%d %08x\n",
  532. word_len, *(rx - 1));
  533. }
  534. } while (c);
  535. }
  536. /* for TX_ONLY mode, be sure all words have shifted out */
  537. if (xfer->rx_buf == NULL) {
  538. if (mcspi_wait_for_reg_bit(chstat_reg,
  539. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  540. dev_err(&spi->dev, "TXS timed out\n");
  541. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  542. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  543. dev_err(&spi->dev, "EOT timed out\n");
  544. /* disable chan to purge rx datas received in TX_ONLY transfer,
  545. * otherwise these rx datas will affect the direct following
  546. * RX_ONLY transfer.
  547. */
  548. omap2_mcspi_set_enable(spi, 0);
  549. }
  550. out:
  551. omap2_mcspi_set_enable(spi, 1);
  552. return count - c;
  553. }
  554. /* called only when no transfer is active to this device */
  555. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  556. struct spi_transfer *t)
  557. {
  558. struct omap2_mcspi_cs *cs = spi->controller_state;
  559. struct omap2_mcspi *mcspi;
  560. struct spi_master *spi_cntrl;
  561. u32 l = 0, div = 0;
  562. u8 word_len = spi->bits_per_word;
  563. u32 speed_hz = spi->max_speed_hz;
  564. mcspi = spi_master_get_devdata(spi->master);
  565. spi_cntrl = mcspi->master;
  566. if (t != NULL && t->bits_per_word)
  567. word_len = t->bits_per_word;
  568. cs->word_len = word_len;
  569. if (t && t->speed_hz)
  570. speed_hz = t->speed_hz;
  571. if (speed_hz) {
  572. while (div <= 15 && (OMAP2_MCSPI_MAX_FREQ / (1 << div))
  573. > speed_hz)
  574. div++;
  575. } else
  576. div = 15;
  577. l = mcspi_cached_chconf0(spi);
  578. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  579. * REVISIT: this controller could support SPI_3WIRE mode.
  580. */
  581. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  582. l |= OMAP2_MCSPI_CHCONF_DPE0;
  583. /* wordlength */
  584. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  585. l |= (word_len - 1) << 7;
  586. /* set chipselect polarity; manage with FORCE */
  587. if (!(spi->mode & SPI_CS_HIGH))
  588. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  589. else
  590. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  591. /* set clock divisor */
  592. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  593. l |= div << 2;
  594. /* set SPI mode 0..3 */
  595. if (spi->mode & SPI_CPOL)
  596. l |= OMAP2_MCSPI_CHCONF_POL;
  597. else
  598. l &= ~OMAP2_MCSPI_CHCONF_POL;
  599. if (spi->mode & SPI_CPHA)
  600. l |= OMAP2_MCSPI_CHCONF_PHA;
  601. else
  602. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  603. mcspi_write_chconf0(spi, l);
  604. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  605. OMAP2_MCSPI_MAX_FREQ / (1 << div),
  606. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  607. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  608. return 0;
  609. }
  610. static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
  611. {
  612. struct spi_device *spi = data;
  613. struct omap2_mcspi *mcspi;
  614. struct omap2_mcspi_dma *mcspi_dma;
  615. mcspi = spi_master_get_devdata(spi->master);
  616. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  617. complete(&mcspi_dma->dma_rx_completion);
  618. /* We must disable the DMA RX request */
  619. omap2_mcspi_set_dma_req(spi, 1, 0);
  620. }
  621. static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
  622. {
  623. struct spi_device *spi = data;
  624. struct omap2_mcspi *mcspi;
  625. struct omap2_mcspi_dma *mcspi_dma;
  626. mcspi = spi_master_get_devdata(spi->master);
  627. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  628. complete(&mcspi_dma->dma_tx_completion);
  629. /* We must disable the DMA TX request */
  630. omap2_mcspi_set_dma_req(spi, 0, 0);
  631. }
  632. static int omap2_mcspi_request_dma(struct spi_device *spi)
  633. {
  634. struct spi_master *master = spi->master;
  635. struct omap2_mcspi *mcspi;
  636. struct omap2_mcspi_dma *mcspi_dma;
  637. mcspi = spi_master_get_devdata(master);
  638. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  639. if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
  640. omap2_mcspi_dma_rx_callback, spi,
  641. &mcspi_dma->dma_rx_channel)) {
  642. dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
  643. return -EAGAIN;
  644. }
  645. if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
  646. omap2_mcspi_dma_tx_callback, spi,
  647. &mcspi_dma->dma_tx_channel)) {
  648. omap_free_dma(mcspi_dma->dma_rx_channel);
  649. mcspi_dma->dma_rx_channel = -1;
  650. dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
  651. return -EAGAIN;
  652. }
  653. init_completion(&mcspi_dma->dma_rx_completion);
  654. init_completion(&mcspi_dma->dma_tx_completion);
  655. return 0;
  656. }
  657. static int omap2_mcspi_setup(struct spi_device *spi)
  658. {
  659. int ret;
  660. struct omap2_mcspi *mcspi;
  661. struct omap2_mcspi_dma *mcspi_dma;
  662. struct omap2_mcspi_cs *cs = spi->controller_state;
  663. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  664. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  665. spi->bits_per_word);
  666. return -EINVAL;
  667. }
  668. mcspi = spi_master_get_devdata(spi->master);
  669. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  670. if (!cs) {
  671. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  672. if (!cs)
  673. return -ENOMEM;
  674. cs->base = mcspi->base + spi->chip_select * 0x14;
  675. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  676. cs->chconf0 = 0;
  677. spi->controller_state = cs;
  678. /* Link this to context save list */
  679. list_add_tail(&cs->node,
  680. &omap2_mcspi_ctx[mcspi->master->bus_num - 1].cs);
  681. }
  682. if (mcspi_dma->dma_rx_channel == -1
  683. || mcspi_dma->dma_tx_channel == -1) {
  684. ret = omap2_mcspi_request_dma(spi);
  685. if (ret < 0)
  686. return ret;
  687. }
  688. if (omap2_mcspi_enable_clocks(mcspi))
  689. return -ENODEV;
  690. ret = omap2_mcspi_setup_transfer(spi, NULL);
  691. omap2_mcspi_disable_clocks(mcspi);
  692. return ret;
  693. }
  694. static void omap2_mcspi_cleanup(struct spi_device *spi)
  695. {
  696. struct omap2_mcspi *mcspi;
  697. struct omap2_mcspi_dma *mcspi_dma;
  698. struct omap2_mcspi_cs *cs;
  699. mcspi = spi_master_get_devdata(spi->master);
  700. if (spi->controller_state) {
  701. /* Unlink controller state from context save list */
  702. cs = spi->controller_state;
  703. list_del(&cs->node);
  704. kfree(spi->controller_state);
  705. }
  706. if (spi->chip_select < spi->master->num_chipselect) {
  707. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  708. if (mcspi_dma->dma_rx_channel != -1) {
  709. omap_free_dma(mcspi_dma->dma_rx_channel);
  710. mcspi_dma->dma_rx_channel = -1;
  711. }
  712. if (mcspi_dma->dma_tx_channel != -1) {
  713. omap_free_dma(mcspi_dma->dma_tx_channel);
  714. mcspi_dma->dma_tx_channel = -1;
  715. }
  716. }
  717. }
  718. static void omap2_mcspi_work(struct work_struct *work)
  719. {
  720. struct omap2_mcspi *mcspi;
  721. mcspi = container_of(work, struct omap2_mcspi, work);
  722. spin_lock_irq(&mcspi->lock);
  723. if (omap2_mcspi_enable_clocks(mcspi))
  724. goto out;
  725. /* We only enable one channel at a time -- the one whose message is
  726. * at the head of the queue -- although this controller would gladly
  727. * arbitrate among multiple channels. This corresponds to "single
  728. * channel" master mode. As a side effect, we need to manage the
  729. * chipselect with the FORCE bit ... CS != channel enable.
  730. */
  731. while (!list_empty(&mcspi->msg_queue)) {
  732. struct spi_message *m;
  733. struct spi_device *spi;
  734. struct spi_transfer *t = NULL;
  735. int cs_active = 0;
  736. struct omap2_mcspi_cs *cs;
  737. struct omap2_mcspi_device_config *cd;
  738. int par_override = 0;
  739. int status = 0;
  740. u32 chconf;
  741. m = container_of(mcspi->msg_queue.next, struct spi_message,
  742. queue);
  743. list_del_init(&m->queue);
  744. spin_unlock_irq(&mcspi->lock);
  745. spi = m->spi;
  746. cs = spi->controller_state;
  747. cd = spi->controller_data;
  748. omap2_mcspi_set_enable(spi, 1);
  749. list_for_each_entry(t, &m->transfers, transfer_list) {
  750. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  751. status = -EINVAL;
  752. break;
  753. }
  754. if (par_override || t->speed_hz || t->bits_per_word) {
  755. par_override = 1;
  756. status = omap2_mcspi_setup_transfer(spi, t);
  757. if (status < 0)
  758. break;
  759. if (!t->speed_hz && !t->bits_per_word)
  760. par_override = 0;
  761. }
  762. if (!cs_active) {
  763. omap2_mcspi_force_cs(spi, 1);
  764. cs_active = 1;
  765. }
  766. chconf = mcspi_cached_chconf0(spi);
  767. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  768. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  769. if (t->tx_buf == NULL)
  770. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  771. else if (t->rx_buf == NULL)
  772. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  773. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  774. /* Turbo mode is for more than one word */
  775. if (t->len > ((cs->word_len + 7) >> 3))
  776. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  777. }
  778. mcspi_write_chconf0(spi, chconf);
  779. if (t->len) {
  780. unsigned count;
  781. /* RX_ONLY mode needs dummy data in TX reg */
  782. if (t->tx_buf == NULL)
  783. __raw_writel(0, cs->base
  784. + OMAP2_MCSPI_TX0);
  785. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  786. count = omap2_mcspi_txrx_dma(spi, t);
  787. else
  788. count = omap2_mcspi_txrx_pio(spi, t);
  789. m->actual_length += count;
  790. if (count != t->len) {
  791. status = -EIO;
  792. break;
  793. }
  794. }
  795. if (t->delay_usecs)
  796. udelay(t->delay_usecs);
  797. /* ignore the "leave it on after last xfer" hint */
  798. if (t->cs_change) {
  799. omap2_mcspi_force_cs(spi, 0);
  800. cs_active = 0;
  801. }
  802. }
  803. /* Restore defaults if they were overriden */
  804. if (par_override) {
  805. par_override = 0;
  806. status = omap2_mcspi_setup_transfer(spi, NULL);
  807. }
  808. if (cs_active)
  809. omap2_mcspi_force_cs(spi, 0);
  810. omap2_mcspi_set_enable(spi, 0);
  811. m->status = status;
  812. m->complete(m->context);
  813. spin_lock_irq(&mcspi->lock);
  814. }
  815. omap2_mcspi_disable_clocks(mcspi);
  816. out:
  817. spin_unlock_irq(&mcspi->lock);
  818. }
  819. static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
  820. {
  821. struct omap2_mcspi *mcspi;
  822. unsigned long flags;
  823. struct spi_transfer *t;
  824. m->actual_length = 0;
  825. m->status = 0;
  826. /* reject invalid messages and transfers */
  827. if (list_empty(&m->transfers) || !m->complete)
  828. return -EINVAL;
  829. list_for_each_entry(t, &m->transfers, transfer_list) {
  830. const void *tx_buf = t->tx_buf;
  831. void *rx_buf = t->rx_buf;
  832. unsigned len = t->len;
  833. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  834. || (len && !(rx_buf || tx_buf))
  835. || (t->bits_per_word &&
  836. ( t->bits_per_word < 4
  837. || t->bits_per_word > 32))) {
  838. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  839. t->speed_hz,
  840. len,
  841. tx_buf ? "tx" : "",
  842. rx_buf ? "rx" : "",
  843. t->bits_per_word);
  844. return -EINVAL;
  845. }
  846. if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
  847. dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
  848. t->speed_hz,
  849. OMAP2_MCSPI_MAX_FREQ/(1<<16));
  850. return -EINVAL;
  851. }
  852. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  853. continue;
  854. if (tx_buf != NULL) {
  855. t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
  856. len, DMA_TO_DEVICE);
  857. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  858. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  859. 'T', len);
  860. return -EINVAL;
  861. }
  862. }
  863. if (rx_buf != NULL) {
  864. t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
  865. DMA_FROM_DEVICE);
  866. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  867. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  868. 'R', len);
  869. if (tx_buf != NULL)
  870. dma_unmap_single(&spi->dev, t->tx_dma,
  871. len, DMA_TO_DEVICE);
  872. return -EINVAL;
  873. }
  874. }
  875. }
  876. mcspi = spi_master_get_devdata(spi->master);
  877. spin_lock_irqsave(&mcspi->lock, flags);
  878. list_add_tail(&m->queue, &mcspi->msg_queue);
  879. queue_work(omap2_mcspi_wq, &mcspi->work);
  880. spin_unlock_irqrestore(&mcspi->lock, flags);
  881. return 0;
  882. }
  883. static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
  884. {
  885. struct spi_master *master = mcspi->master;
  886. u32 tmp;
  887. if (omap2_mcspi_enable_clocks(mcspi))
  888. return -1;
  889. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  890. OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
  891. do {
  892. tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
  893. } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
  894. tmp = OMAP2_MCSPI_SYSCONFIG_AUTOIDLE |
  895. OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP |
  896. OMAP2_MCSPI_SYSCONFIG_SMARTIDLE;
  897. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG, tmp);
  898. omap2_mcspi_ctx[master->bus_num - 1].sysconfig = tmp;
  899. tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  900. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
  901. omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
  902. omap2_mcspi_set_master_mode(master);
  903. omap2_mcspi_disable_clocks(mcspi);
  904. return 0;
  905. }
  906. static u8 __initdata spi1_rxdma_id [] = {
  907. OMAP24XX_DMA_SPI1_RX0,
  908. OMAP24XX_DMA_SPI1_RX1,
  909. OMAP24XX_DMA_SPI1_RX2,
  910. OMAP24XX_DMA_SPI1_RX3,
  911. };
  912. static u8 __initdata spi1_txdma_id [] = {
  913. OMAP24XX_DMA_SPI1_TX0,
  914. OMAP24XX_DMA_SPI1_TX1,
  915. OMAP24XX_DMA_SPI1_TX2,
  916. OMAP24XX_DMA_SPI1_TX3,
  917. };
  918. static u8 __initdata spi2_rxdma_id[] = {
  919. OMAP24XX_DMA_SPI2_RX0,
  920. OMAP24XX_DMA_SPI2_RX1,
  921. };
  922. static u8 __initdata spi2_txdma_id[] = {
  923. OMAP24XX_DMA_SPI2_TX0,
  924. OMAP24XX_DMA_SPI2_TX1,
  925. };
  926. #if defined(CONFIG_SOC_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
  927. || defined(CONFIG_ARCH_OMAP4)
  928. static u8 __initdata spi3_rxdma_id[] = {
  929. OMAP24XX_DMA_SPI3_RX0,
  930. OMAP24XX_DMA_SPI3_RX1,
  931. };
  932. static u8 __initdata spi3_txdma_id[] = {
  933. OMAP24XX_DMA_SPI3_TX0,
  934. OMAP24XX_DMA_SPI3_TX1,
  935. };
  936. #endif
  937. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  938. static u8 __initdata spi4_rxdma_id[] = {
  939. OMAP34XX_DMA_SPI4_RX0,
  940. };
  941. static u8 __initdata spi4_txdma_id[] = {
  942. OMAP34XX_DMA_SPI4_TX0,
  943. };
  944. #endif
  945. static int __init omap2_mcspi_probe(struct platform_device *pdev)
  946. {
  947. struct spi_master *master;
  948. struct omap2_mcspi *mcspi;
  949. struct resource *r;
  950. int status = 0, i;
  951. const u8 *rxdma_id, *txdma_id;
  952. unsigned num_chipselect;
  953. switch (pdev->id) {
  954. case 1:
  955. rxdma_id = spi1_rxdma_id;
  956. txdma_id = spi1_txdma_id;
  957. num_chipselect = 4;
  958. break;
  959. case 2:
  960. rxdma_id = spi2_rxdma_id;
  961. txdma_id = spi2_txdma_id;
  962. num_chipselect = 2;
  963. break;
  964. #if defined(CONFIG_SOC_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
  965. || defined(CONFIG_ARCH_OMAP4)
  966. case 3:
  967. rxdma_id = spi3_rxdma_id;
  968. txdma_id = spi3_txdma_id;
  969. num_chipselect = 2;
  970. break;
  971. #endif
  972. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  973. case 4:
  974. rxdma_id = spi4_rxdma_id;
  975. txdma_id = spi4_txdma_id;
  976. num_chipselect = 1;
  977. break;
  978. #endif
  979. default:
  980. return -EINVAL;
  981. }
  982. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  983. if (master == NULL) {
  984. dev_dbg(&pdev->dev, "master allocation failed\n");
  985. return -ENOMEM;
  986. }
  987. /* the spi->mode bits understood by this driver: */
  988. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  989. if (pdev->id != -1)
  990. master->bus_num = pdev->id;
  991. master->setup = omap2_mcspi_setup;
  992. master->transfer = omap2_mcspi_transfer;
  993. master->cleanup = omap2_mcspi_cleanup;
  994. master->num_chipselect = num_chipselect;
  995. dev_set_drvdata(&pdev->dev, master);
  996. mcspi = spi_master_get_devdata(master);
  997. mcspi->master = master;
  998. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  999. if (r == NULL) {
  1000. status = -ENODEV;
  1001. goto err1;
  1002. }
  1003. if (!request_mem_region(r->start, (r->end - r->start) + 1,
  1004. dev_name(&pdev->dev))) {
  1005. status = -EBUSY;
  1006. goto err1;
  1007. }
  1008. mcspi->phys = r->start;
  1009. mcspi->base = ioremap(r->start, r->end - r->start + 1);
  1010. if (!mcspi->base) {
  1011. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  1012. status = -ENOMEM;
  1013. goto err1aa;
  1014. }
  1015. INIT_WORK(&mcspi->work, omap2_mcspi_work);
  1016. spin_lock_init(&mcspi->lock);
  1017. INIT_LIST_HEAD(&mcspi->msg_queue);
  1018. INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs);
  1019. mcspi->ick = clk_get(&pdev->dev, "ick");
  1020. if (IS_ERR(mcspi->ick)) {
  1021. dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
  1022. status = PTR_ERR(mcspi->ick);
  1023. goto err1a;
  1024. }
  1025. mcspi->fck = clk_get(&pdev->dev, "fck");
  1026. if (IS_ERR(mcspi->fck)) {
  1027. dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
  1028. status = PTR_ERR(mcspi->fck);
  1029. goto err2;
  1030. }
  1031. mcspi->dma_channels = kcalloc(master->num_chipselect,
  1032. sizeof(struct omap2_mcspi_dma),
  1033. GFP_KERNEL);
  1034. if (mcspi->dma_channels == NULL)
  1035. goto err3;
  1036. for (i = 0; i < num_chipselect; i++) {
  1037. mcspi->dma_channels[i].dma_rx_channel = -1;
  1038. mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
  1039. mcspi->dma_channels[i].dma_tx_channel = -1;
  1040. mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
  1041. }
  1042. if (omap2_mcspi_reset(mcspi) < 0)
  1043. goto err4;
  1044. status = spi_register_master(master);
  1045. if (status < 0)
  1046. goto err4;
  1047. return status;
  1048. err4:
  1049. kfree(mcspi->dma_channels);
  1050. err3:
  1051. clk_put(mcspi->fck);
  1052. err2:
  1053. clk_put(mcspi->ick);
  1054. err1a:
  1055. iounmap(mcspi->base);
  1056. err1aa:
  1057. release_mem_region(r->start, (r->end - r->start) + 1);
  1058. err1:
  1059. spi_master_put(master);
  1060. return status;
  1061. }
  1062. static int __exit omap2_mcspi_remove(struct platform_device *pdev)
  1063. {
  1064. struct spi_master *master;
  1065. struct omap2_mcspi *mcspi;
  1066. struct omap2_mcspi_dma *dma_channels;
  1067. struct resource *r;
  1068. void __iomem *base;
  1069. master = dev_get_drvdata(&pdev->dev);
  1070. mcspi = spi_master_get_devdata(master);
  1071. dma_channels = mcspi->dma_channels;
  1072. clk_put(mcspi->fck);
  1073. clk_put(mcspi->ick);
  1074. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1075. release_mem_region(r->start, (r->end - r->start) + 1);
  1076. base = mcspi->base;
  1077. spi_unregister_master(master);
  1078. iounmap(base);
  1079. kfree(dma_channels);
  1080. return 0;
  1081. }
  1082. /* work with hotplug and coldplug */
  1083. MODULE_ALIAS("platform:omap2_mcspi");
  1084. #ifdef CONFIG_SUSPEND
  1085. /*
  1086. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1087. * unactive state when driver was suspend, then force it to unactive state at
  1088. * wake up.
  1089. */
  1090. static int omap2_mcspi_resume(struct device *dev)
  1091. {
  1092. struct spi_master *master = dev_get_drvdata(dev);
  1093. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1094. struct omap2_mcspi_cs *cs;
  1095. omap2_mcspi_enable_clocks(mcspi);
  1096. list_for_each_entry(cs, &omap2_mcspi_ctx[master->bus_num - 1].cs,
  1097. node) {
  1098. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1099. /*
  1100. * We need to toggle CS state for OMAP take this
  1101. * change in account.
  1102. */
  1103. MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
  1104. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1105. MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
  1106. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1107. }
  1108. }
  1109. omap2_mcspi_disable_clocks(mcspi);
  1110. return 0;
  1111. }
  1112. #else
  1113. #define omap2_mcspi_resume NULL
  1114. #endif
  1115. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1116. .resume = omap2_mcspi_resume,
  1117. };
  1118. static struct platform_driver omap2_mcspi_driver = {
  1119. .driver = {
  1120. .name = "omap2_mcspi",
  1121. .owner = THIS_MODULE,
  1122. .pm = &omap2_mcspi_pm_ops
  1123. },
  1124. .remove = __exit_p(omap2_mcspi_remove),
  1125. };
  1126. static int __init omap2_mcspi_init(void)
  1127. {
  1128. omap2_mcspi_wq = create_singlethread_workqueue(
  1129. omap2_mcspi_driver.driver.name);
  1130. if (omap2_mcspi_wq == NULL)
  1131. return -1;
  1132. return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
  1133. }
  1134. subsys_initcall(omap2_mcspi_init);
  1135. static void __exit omap2_mcspi_exit(void)
  1136. {
  1137. platform_driver_unregister(&omap2_mcspi_driver);
  1138. destroy_workqueue(omap2_mcspi_wq);
  1139. }
  1140. module_exit(omap2_mcspi_exit);
  1141. MODULE_LICENSE("GPL");