spi-omap2-mcspi.c 32 KB

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