spi-omap2-mcspi.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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/dmaengine.h>
  31. #include <linux/omap-dma.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/err.h>
  34. #include <linux/clk.h>
  35. #include <linux/io.h>
  36. #include <linux/slab.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/of.h>
  39. #include <linux/of_device.h>
  40. #include <linux/spi/spi.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. struct dma_chan *dma_tx;
  84. struct dma_chan *dma_rx;
  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 void omap2_mcspi_rx_callback(void *data)
  249. {
  250. struct spi_device *spi = data;
  251. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  252. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  253. complete(&mcspi_dma->dma_rx_completion);
  254. /* We must disable the DMA RX request */
  255. omap2_mcspi_set_dma_req(spi, 1, 0);
  256. }
  257. static void omap2_mcspi_tx_callback(void *data)
  258. {
  259. struct spi_device *spi = data;
  260. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  261. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  262. complete(&mcspi_dma->dma_tx_completion);
  263. /* We must disable the DMA TX request */
  264. omap2_mcspi_set_dma_req(spi, 0, 0);
  265. }
  266. static unsigned
  267. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  268. {
  269. struct omap2_mcspi *mcspi;
  270. struct omap2_mcspi_cs *cs = spi->controller_state;
  271. struct omap2_mcspi_dma *mcspi_dma;
  272. unsigned int count;
  273. int word_len, element_count;
  274. int elements = 0;
  275. u32 l;
  276. u8 * rx;
  277. const u8 * tx;
  278. void __iomem *chstat_reg;
  279. struct dma_slave_config cfg;
  280. enum dma_slave_buswidth width;
  281. unsigned es;
  282. mcspi = spi_master_get_devdata(spi->master);
  283. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  284. l = mcspi_cached_chconf0(spi);
  285. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  286. if (cs->word_len <= 8) {
  287. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  288. es = 1;
  289. } else if (cs->word_len <= 16) {
  290. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  291. es = 2;
  292. } else {
  293. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  294. es = 4;
  295. }
  296. memset(&cfg, 0, sizeof(cfg));
  297. cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
  298. cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
  299. cfg.src_addr_width = width;
  300. cfg.dst_addr_width = width;
  301. cfg.src_maxburst = 1;
  302. cfg.dst_maxburst = 1;
  303. if (xfer->tx_buf && mcspi_dma->dma_tx) {
  304. struct dma_async_tx_descriptor *tx;
  305. struct scatterlist sg;
  306. dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
  307. sg_init_table(&sg, 1);
  308. sg_dma_address(&sg) = xfer->tx_dma;
  309. sg_dma_len(&sg) = xfer->len;
  310. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
  311. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  312. if (tx) {
  313. tx->callback = omap2_mcspi_tx_callback;
  314. tx->callback_param = spi;
  315. dmaengine_submit(tx);
  316. } else {
  317. /* FIXME: fall back to PIO? */
  318. }
  319. }
  320. if (xfer->rx_buf && mcspi_dma->dma_rx) {
  321. struct dma_async_tx_descriptor *tx;
  322. struct scatterlist sg;
  323. size_t len = xfer->len - es;
  324. dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
  325. if (l & OMAP2_MCSPI_CHCONF_TURBO)
  326. len -= es;
  327. sg_init_table(&sg, 1);
  328. sg_dma_address(&sg) = xfer->rx_dma;
  329. sg_dma_len(&sg) = len;
  330. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
  331. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  332. if (tx) {
  333. tx->callback = omap2_mcspi_rx_callback;
  334. tx->callback_param = spi;
  335. dmaengine_submit(tx);
  336. } else {
  337. /* FIXME: fall back to PIO? */
  338. }
  339. }
  340. count = xfer->len;
  341. word_len = cs->word_len;
  342. rx = xfer->rx_buf;
  343. tx = xfer->tx_buf;
  344. if (word_len <= 8) {
  345. element_count = count;
  346. } else if (word_len <= 16) {
  347. element_count = count >> 1;
  348. } else /* word_len <= 32 */ {
  349. element_count = count >> 2;
  350. }
  351. if (tx != NULL) {
  352. dma_async_issue_pending(mcspi_dma->dma_tx);
  353. omap2_mcspi_set_dma_req(spi, 0, 1);
  354. }
  355. if (rx != NULL) {
  356. dma_async_issue_pending(mcspi_dma->dma_rx);
  357. omap2_mcspi_set_dma_req(spi, 1, 1);
  358. }
  359. if (tx != NULL) {
  360. wait_for_completion(&mcspi_dma->dma_tx_completion);
  361. dma_unmap_single(mcspi->dev, xfer->tx_dma, count,
  362. DMA_TO_DEVICE);
  363. /* for TX_ONLY mode, be sure all words have shifted out */
  364. if (rx == NULL) {
  365. if (mcspi_wait_for_reg_bit(chstat_reg,
  366. OMAP2_MCSPI_CHSTAT_TXS) < 0)
  367. dev_err(&spi->dev, "TXS timed out\n");
  368. else if (mcspi_wait_for_reg_bit(chstat_reg,
  369. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  370. dev_err(&spi->dev, "EOT timed out\n");
  371. }
  372. }
  373. if (rx != NULL) {
  374. wait_for_completion(&mcspi_dma->dma_rx_completion);
  375. dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
  376. DMA_FROM_DEVICE);
  377. omap2_mcspi_set_enable(spi, 0);
  378. elements = element_count - 1;
  379. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  380. elements--;
  381. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  382. & OMAP2_MCSPI_CHSTAT_RXS)) {
  383. u32 w;
  384. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  385. if (word_len <= 8)
  386. ((u8 *)xfer->rx_buf)[elements++] = w;
  387. else if (word_len <= 16)
  388. ((u16 *)xfer->rx_buf)[elements++] = w;
  389. else /* word_len <= 32 */
  390. ((u32 *)xfer->rx_buf)[elements++] = w;
  391. } else {
  392. dev_err(&spi->dev,
  393. "DMA RX penultimate word empty");
  394. count -= (word_len <= 8) ? 2 :
  395. (word_len <= 16) ? 4 :
  396. /* word_len <= 32 */ 8;
  397. omap2_mcspi_set_enable(spi, 1);
  398. return count;
  399. }
  400. }
  401. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  402. & OMAP2_MCSPI_CHSTAT_RXS)) {
  403. u32 w;
  404. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  405. if (word_len <= 8)
  406. ((u8 *)xfer->rx_buf)[elements] = w;
  407. else if (word_len <= 16)
  408. ((u16 *)xfer->rx_buf)[elements] = w;
  409. else /* word_len <= 32 */
  410. ((u32 *)xfer->rx_buf)[elements] = w;
  411. } else {
  412. dev_err(&spi->dev, "DMA RX last word empty");
  413. count -= (word_len <= 8) ? 1 :
  414. (word_len <= 16) ? 2 :
  415. /* word_len <= 32 */ 4;
  416. }
  417. omap2_mcspi_set_enable(spi, 1);
  418. }
  419. return count;
  420. }
  421. static unsigned
  422. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  423. {
  424. struct omap2_mcspi *mcspi;
  425. struct omap2_mcspi_cs *cs = spi->controller_state;
  426. unsigned int count, c;
  427. u32 l;
  428. void __iomem *base = cs->base;
  429. void __iomem *tx_reg;
  430. void __iomem *rx_reg;
  431. void __iomem *chstat_reg;
  432. int word_len;
  433. mcspi = spi_master_get_devdata(spi->master);
  434. count = xfer->len;
  435. c = count;
  436. word_len = cs->word_len;
  437. l = mcspi_cached_chconf0(spi);
  438. /* We store the pre-calculated register addresses on stack to speed
  439. * up the transfer loop. */
  440. tx_reg = base + OMAP2_MCSPI_TX0;
  441. rx_reg = base + OMAP2_MCSPI_RX0;
  442. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  443. if (c < (word_len>>3))
  444. return 0;
  445. if (word_len <= 8) {
  446. u8 *rx;
  447. const u8 *tx;
  448. rx = xfer->rx_buf;
  449. tx = xfer->tx_buf;
  450. do {
  451. c -= 1;
  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 %02x\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 == 1 && 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 %02x\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 %02x\n",
  486. word_len, *(rx - 1));
  487. }
  488. } while (c);
  489. } else if (word_len <= 16) {
  490. u16 *rx;
  491. const u16 *tx;
  492. rx = xfer->rx_buf;
  493. tx = xfer->tx_buf;
  494. do {
  495. c -= 2;
  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 %04x\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 == 2 && 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 %04x\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 %04x\n",
  530. word_len, *(rx - 1));
  531. }
  532. } while (c >= 2);
  533. } else if (word_len <= 32) {
  534. u32 *rx;
  535. const u32 *tx;
  536. rx = xfer->rx_buf;
  537. tx = xfer->tx_buf;
  538. do {
  539. c -= 4;
  540. if (tx != NULL) {
  541. if (mcspi_wait_for_reg_bit(chstat_reg,
  542. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  543. dev_err(&spi->dev, "TXS timed out\n");
  544. goto out;
  545. }
  546. dev_vdbg(&spi->dev, "write-%d %08x\n",
  547. word_len, *tx);
  548. __raw_writel(*tx++, tx_reg);
  549. }
  550. if (rx != NULL) {
  551. if (mcspi_wait_for_reg_bit(chstat_reg,
  552. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  553. dev_err(&spi->dev, "RXS timed out\n");
  554. goto out;
  555. }
  556. if (c == 4 && tx == NULL &&
  557. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  558. omap2_mcspi_set_enable(spi, 0);
  559. *rx++ = __raw_readl(rx_reg);
  560. dev_vdbg(&spi->dev, "read-%d %08x\n",
  561. word_len, *(rx - 1));
  562. if (mcspi_wait_for_reg_bit(chstat_reg,
  563. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  564. dev_err(&spi->dev,
  565. "RXS timed out\n");
  566. goto out;
  567. }
  568. c = 0;
  569. } else if (c == 0 && tx == NULL) {
  570. omap2_mcspi_set_enable(spi, 0);
  571. }
  572. *rx++ = __raw_readl(rx_reg);
  573. dev_vdbg(&spi->dev, "read-%d %08x\n",
  574. word_len, *(rx - 1));
  575. }
  576. } while (c >= 4);
  577. }
  578. /* for TX_ONLY mode, be sure all words have shifted out */
  579. if (xfer->rx_buf == NULL) {
  580. if (mcspi_wait_for_reg_bit(chstat_reg,
  581. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  582. dev_err(&spi->dev, "TXS timed out\n");
  583. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  584. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  585. dev_err(&spi->dev, "EOT timed out\n");
  586. /* disable chan to purge rx datas received in TX_ONLY transfer,
  587. * otherwise these rx datas will affect the direct following
  588. * RX_ONLY transfer.
  589. */
  590. omap2_mcspi_set_enable(spi, 0);
  591. }
  592. out:
  593. omap2_mcspi_set_enable(spi, 1);
  594. return count - c;
  595. }
  596. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  597. {
  598. u32 div;
  599. for (div = 0; div < 15; div++)
  600. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  601. return div;
  602. return 15;
  603. }
  604. /* called only when no transfer is active to this device */
  605. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  606. struct spi_transfer *t)
  607. {
  608. struct omap2_mcspi_cs *cs = spi->controller_state;
  609. struct omap2_mcspi *mcspi;
  610. struct spi_master *spi_cntrl;
  611. u32 l = 0, div = 0;
  612. u8 word_len = spi->bits_per_word;
  613. u32 speed_hz = spi->max_speed_hz;
  614. mcspi = spi_master_get_devdata(spi->master);
  615. spi_cntrl = mcspi->master;
  616. if (t != NULL && t->bits_per_word)
  617. word_len = t->bits_per_word;
  618. cs->word_len = word_len;
  619. if (t && t->speed_hz)
  620. speed_hz = t->speed_hz;
  621. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  622. div = omap2_mcspi_calc_divisor(speed_hz);
  623. l = mcspi_cached_chconf0(spi);
  624. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  625. * REVISIT: this controller could support SPI_3WIRE mode.
  626. */
  627. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  628. l |= OMAP2_MCSPI_CHCONF_DPE0;
  629. /* wordlength */
  630. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  631. l |= (word_len - 1) << 7;
  632. /* set chipselect polarity; manage with FORCE */
  633. if (!(spi->mode & SPI_CS_HIGH))
  634. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  635. else
  636. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  637. /* set clock divisor */
  638. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  639. l |= div << 2;
  640. /* set SPI mode 0..3 */
  641. if (spi->mode & SPI_CPOL)
  642. l |= OMAP2_MCSPI_CHCONF_POL;
  643. else
  644. l &= ~OMAP2_MCSPI_CHCONF_POL;
  645. if (spi->mode & SPI_CPHA)
  646. l |= OMAP2_MCSPI_CHCONF_PHA;
  647. else
  648. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  649. mcspi_write_chconf0(spi, l);
  650. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  651. OMAP2_MCSPI_MAX_FREQ >> div,
  652. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  653. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  654. return 0;
  655. }
  656. static int omap2_mcspi_request_dma(struct spi_device *spi)
  657. {
  658. struct spi_master *master = spi->master;
  659. struct omap2_mcspi *mcspi;
  660. struct omap2_mcspi_dma *mcspi_dma;
  661. dma_cap_mask_t mask;
  662. unsigned sig;
  663. mcspi = spi_master_get_devdata(master);
  664. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  665. init_completion(&mcspi_dma->dma_rx_completion);
  666. init_completion(&mcspi_dma->dma_tx_completion);
  667. dma_cap_zero(mask);
  668. dma_cap_set(DMA_SLAVE, mask);
  669. sig = mcspi_dma->dma_rx_sync_dev;
  670. mcspi_dma->dma_rx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  671. if (!mcspi_dma->dma_rx) {
  672. dev_err(&spi->dev, "no RX DMA engine channel for McSPI\n");
  673. return -EAGAIN;
  674. }
  675. sig = mcspi_dma->dma_tx_sync_dev;
  676. mcspi_dma->dma_tx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  677. if (!mcspi_dma->dma_tx) {
  678. dev_err(&spi->dev, "no TX DMA engine channel for McSPI\n");
  679. dma_release_channel(mcspi_dma->dma_rx);
  680. mcspi_dma->dma_rx = NULL;
  681. return -EAGAIN;
  682. }
  683. return 0;
  684. }
  685. static int omap2_mcspi_setup(struct spi_device *spi)
  686. {
  687. int ret;
  688. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  689. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  690. struct omap2_mcspi_dma *mcspi_dma;
  691. struct omap2_mcspi_cs *cs = spi->controller_state;
  692. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  693. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  694. spi->bits_per_word);
  695. return -EINVAL;
  696. }
  697. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  698. if (!cs) {
  699. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  700. if (!cs)
  701. return -ENOMEM;
  702. cs->base = mcspi->base + spi->chip_select * 0x14;
  703. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  704. cs->chconf0 = 0;
  705. spi->controller_state = cs;
  706. /* Link this to context save list */
  707. list_add_tail(&cs->node, &ctx->cs);
  708. }
  709. if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
  710. ret = omap2_mcspi_request_dma(spi);
  711. if (ret < 0)
  712. return ret;
  713. }
  714. ret = omap2_mcspi_enable_clocks(mcspi);
  715. if (ret < 0)
  716. return ret;
  717. ret = omap2_mcspi_setup_transfer(spi, NULL);
  718. omap2_mcspi_disable_clocks(mcspi);
  719. return ret;
  720. }
  721. static void omap2_mcspi_cleanup(struct spi_device *spi)
  722. {
  723. struct omap2_mcspi *mcspi;
  724. struct omap2_mcspi_dma *mcspi_dma;
  725. struct omap2_mcspi_cs *cs;
  726. mcspi = spi_master_get_devdata(spi->master);
  727. if (spi->controller_state) {
  728. /* Unlink controller state from context save list */
  729. cs = spi->controller_state;
  730. list_del(&cs->node);
  731. kfree(cs);
  732. }
  733. if (spi->chip_select < spi->master->num_chipselect) {
  734. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  735. if (mcspi_dma->dma_rx) {
  736. dma_release_channel(mcspi_dma->dma_rx);
  737. mcspi_dma->dma_rx = NULL;
  738. }
  739. if (mcspi_dma->dma_tx) {
  740. dma_release_channel(mcspi_dma->dma_tx);
  741. mcspi_dma->dma_tx = NULL;
  742. }
  743. }
  744. }
  745. static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
  746. {
  747. /* We only enable one channel at a time -- the one whose message is
  748. * -- although this controller would gladly
  749. * arbitrate among multiple channels. This corresponds to "single
  750. * channel" master mode. As a side effect, we need to manage the
  751. * chipselect with the FORCE bit ... CS != channel enable.
  752. */
  753. struct spi_device *spi;
  754. struct spi_transfer *t = NULL;
  755. int cs_active = 0;
  756. struct omap2_mcspi_cs *cs;
  757. struct omap2_mcspi_device_config *cd;
  758. int par_override = 0;
  759. int status = 0;
  760. u32 chconf;
  761. spi = m->spi;
  762. cs = spi->controller_state;
  763. cd = spi->controller_data;
  764. omap2_mcspi_set_enable(spi, 1);
  765. list_for_each_entry(t, &m->transfers, transfer_list) {
  766. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  767. status = -EINVAL;
  768. break;
  769. }
  770. if (par_override || t->speed_hz || t->bits_per_word) {
  771. par_override = 1;
  772. status = omap2_mcspi_setup_transfer(spi, t);
  773. if (status < 0)
  774. break;
  775. if (!t->speed_hz && !t->bits_per_word)
  776. par_override = 0;
  777. }
  778. if (!cs_active) {
  779. omap2_mcspi_force_cs(spi, 1);
  780. cs_active = 1;
  781. }
  782. chconf = mcspi_cached_chconf0(spi);
  783. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  784. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  785. if (t->tx_buf == NULL)
  786. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  787. else if (t->rx_buf == NULL)
  788. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  789. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  790. /* Turbo mode is for more than one word */
  791. if (t->len > ((cs->word_len + 7) >> 3))
  792. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  793. }
  794. mcspi_write_chconf0(spi, chconf);
  795. if (t->len) {
  796. unsigned count;
  797. /* RX_ONLY mode needs dummy data in TX reg */
  798. if (t->tx_buf == NULL)
  799. __raw_writel(0, cs->base
  800. + OMAP2_MCSPI_TX0);
  801. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  802. count = omap2_mcspi_txrx_dma(spi, t);
  803. else
  804. count = omap2_mcspi_txrx_pio(spi, t);
  805. m->actual_length += count;
  806. if (count != t->len) {
  807. status = -EIO;
  808. break;
  809. }
  810. }
  811. if (t->delay_usecs)
  812. udelay(t->delay_usecs);
  813. /* ignore the "leave it on after last xfer" hint */
  814. if (t->cs_change) {
  815. omap2_mcspi_force_cs(spi, 0);
  816. cs_active = 0;
  817. }
  818. }
  819. /* Restore defaults if they were overriden */
  820. if (par_override) {
  821. par_override = 0;
  822. status = omap2_mcspi_setup_transfer(spi, NULL);
  823. }
  824. if (cs_active)
  825. omap2_mcspi_force_cs(spi, 0);
  826. omap2_mcspi_set_enable(spi, 0);
  827. m->status = status;
  828. }
  829. static int omap2_mcspi_transfer_one_message(struct spi_master *master,
  830. struct spi_message *m)
  831. {
  832. struct omap2_mcspi *mcspi;
  833. struct spi_transfer *t;
  834. mcspi = spi_master_get_devdata(master);
  835. m->actual_length = 0;
  836. m->status = 0;
  837. /* reject invalid messages and transfers */
  838. if (list_empty(&m->transfers))
  839. return -EINVAL;
  840. list_for_each_entry(t, &m->transfers, transfer_list) {
  841. const void *tx_buf = t->tx_buf;
  842. void *rx_buf = t->rx_buf;
  843. unsigned len = t->len;
  844. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  845. || (len && !(rx_buf || tx_buf))
  846. || (t->bits_per_word &&
  847. ( t->bits_per_word < 4
  848. || t->bits_per_word > 32))) {
  849. dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  850. t->speed_hz,
  851. len,
  852. tx_buf ? "tx" : "",
  853. rx_buf ? "rx" : "",
  854. t->bits_per_word);
  855. return -EINVAL;
  856. }
  857. if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
  858. dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
  859. t->speed_hz,
  860. OMAP2_MCSPI_MAX_FREQ >> 15);
  861. return -EINVAL;
  862. }
  863. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  864. continue;
  865. if (tx_buf != NULL) {
  866. t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
  867. len, DMA_TO_DEVICE);
  868. if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
  869. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  870. 'T', len);
  871. return -EINVAL;
  872. }
  873. }
  874. if (rx_buf != NULL) {
  875. t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
  876. DMA_FROM_DEVICE);
  877. if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
  878. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  879. 'R', len);
  880. if (tx_buf != NULL)
  881. dma_unmap_single(mcspi->dev, t->tx_dma,
  882. len, DMA_TO_DEVICE);
  883. return -EINVAL;
  884. }
  885. }
  886. }
  887. omap2_mcspi_work(mcspi, m);
  888. spi_finalize_current_message(master);
  889. return 0;
  890. }
  891. static int __devinit omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
  892. {
  893. struct spi_master *master = mcspi->master;
  894. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  895. int ret = 0;
  896. ret = omap2_mcspi_enable_clocks(mcspi);
  897. if (ret < 0)
  898. return ret;
  899. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
  900. OMAP2_MCSPI_WAKEUPENABLE_WKEN);
  901. ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  902. omap2_mcspi_set_master_mode(master);
  903. omap2_mcspi_disable_clocks(mcspi);
  904. return 0;
  905. }
  906. static int omap_mcspi_runtime_resume(struct device *dev)
  907. {
  908. struct omap2_mcspi *mcspi;
  909. struct spi_master *master;
  910. master = dev_get_drvdata(dev);
  911. mcspi = spi_master_get_devdata(master);
  912. omap2_mcspi_restore_ctx(mcspi);
  913. return 0;
  914. }
  915. static struct omap2_mcspi_platform_config omap2_pdata = {
  916. .regs_offset = 0,
  917. };
  918. static struct omap2_mcspi_platform_config omap4_pdata = {
  919. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  920. };
  921. static const struct of_device_id omap_mcspi_of_match[] = {
  922. {
  923. .compatible = "ti,omap2-mcspi",
  924. .data = &omap2_pdata,
  925. },
  926. {
  927. .compatible = "ti,omap4-mcspi",
  928. .data = &omap4_pdata,
  929. },
  930. { },
  931. };
  932. MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
  933. static int __devinit omap2_mcspi_probe(struct platform_device *pdev)
  934. {
  935. struct spi_master *master;
  936. struct omap2_mcspi_platform_config *pdata;
  937. struct omap2_mcspi *mcspi;
  938. struct resource *r;
  939. int status = 0, i;
  940. u32 regs_offset = 0;
  941. static int bus_num = 1;
  942. struct device_node *node = pdev->dev.of_node;
  943. const struct of_device_id *match;
  944. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  945. if (master == NULL) {
  946. dev_dbg(&pdev->dev, "master allocation failed\n");
  947. return -ENOMEM;
  948. }
  949. /* the spi->mode bits understood by this driver: */
  950. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  951. master->setup = omap2_mcspi_setup;
  952. master->prepare_transfer_hardware = omap2_prepare_transfer;
  953. master->unprepare_transfer_hardware = omap2_unprepare_transfer;
  954. master->transfer_one_message = omap2_mcspi_transfer_one_message;
  955. master->cleanup = omap2_mcspi_cleanup;
  956. master->dev.of_node = node;
  957. match = of_match_device(omap_mcspi_of_match, &pdev->dev);
  958. if (match) {
  959. u32 num_cs = 1; /* default number of chipselect */
  960. pdata = match->data;
  961. of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
  962. master->num_chipselect = num_cs;
  963. master->bus_num = bus_num++;
  964. } else {
  965. pdata = pdev->dev.platform_data;
  966. master->num_chipselect = pdata->num_cs;
  967. if (pdev->id != -1)
  968. master->bus_num = pdev->id;
  969. }
  970. regs_offset = pdata->regs_offset;
  971. dev_set_drvdata(&pdev->dev, master);
  972. mcspi = spi_master_get_devdata(master);
  973. mcspi->master = master;
  974. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  975. if (r == NULL) {
  976. status = -ENODEV;
  977. goto free_master;
  978. }
  979. r->start += regs_offset;
  980. r->end += regs_offset;
  981. mcspi->phys = r->start;
  982. mcspi->base = devm_request_and_ioremap(&pdev->dev, r);
  983. if (!mcspi->base) {
  984. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  985. status = -ENOMEM;
  986. goto free_master;
  987. }
  988. mcspi->dev = &pdev->dev;
  989. INIT_LIST_HEAD(&mcspi->ctx.cs);
  990. mcspi->dma_channels = kcalloc(master->num_chipselect,
  991. sizeof(struct omap2_mcspi_dma),
  992. GFP_KERNEL);
  993. if (mcspi->dma_channels == NULL)
  994. goto free_master;
  995. for (i = 0; i < master->num_chipselect; i++) {
  996. char dma_ch_name[14];
  997. struct resource *dma_res;
  998. sprintf(dma_ch_name, "rx%d", i);
  999. dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  1000. dma_ch_name);
  1001. if (!dma_res) {
  1002. dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
  1003. status = -ENODEV;
  1004. break;
  1005. }
  1006. mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
  1007. sprintf(dma_ch_name, "tx%d", i);
  1008. dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  1009. dma_ch_name);
  1010. if (!dma_res) {
  1011. dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
  1012. status = -ENODEV;
  1013. break;
  1014. }
  1015. mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
  1016. }
  1017. if (status < 0)
  1018. goto dma_chnl_free;
  1019. pm_runtime_use_autosuspend(&pdev->dev);
  1020. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  1021. pm_runtime_enable(&pdev->dev);
  1022. if (status || omap2_mcspi_master_setup(mcspi) < 0)
  1023. goto disable_pm;
  1024. status = spi_register_master(master);
  1025. if (status < 0)
  1026. goto disable_pm;
  1027. return status;
  1028. disable_pm:
  1029. pm_runtime_disable(&pdev->dev);
  1030. dma_chnl_free:
  1031. kfree(mcspi->dma_channels);
  1032. free_master:
  1033. spi_master_put(master);
  1034. platform_set_drvdata(pdev, NULL);
  1035. return status;
  1036. }
  1037. static int __devexit omap2_mcspi_remove(struct platform_device *pdev)
  1038. {
  1039. struct spi_master *master;
  1040. struct omap2_mcspi *mcspi;
  1041. struct omap2_mcspi_dma *dma_channels;
  1042. master = dev_get_drvdata(&pdev->dev);
  1043. mcspi = spi_master_get_devdata(master);
  1044. dma_channels = mcspi->dma_channels;
  1045. omap2_mcspi_disable_clocks(mcspi);
  1046. pm_runtime_disable(&pdev->dev);
  1047. spi_unregister_master(master);
  1048. kfree(dma_channels);
  1049. platform_set_drvdata(pdev, NULL);
  1050. return 0;
  1051. }
  1052. /* work with hotplug and coldplug */
  1053. MODULE_ALIAS("platform:omap2_mcspi");
  1054. #ifdef CONFIG_SUSPEND
  1055. /*
  1056. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1057. * unactive state when driver was suspend, then force it to unactive state at
  1058. * wake up.
  1059. */
  1060. static int omap2_mcspi_resume(struct device *dev)
  1061. {
  1062. struct spi_master *master = dev_get_drvdata(dev);
  1063. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1064. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1065. struct omap2_mcspi_cs *cs;
  1066. omap2_mcspi_enable_clocks(mcspi);
  1067. list_for_each_entry(cs, &ctx->cs, node) {
  1068. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1069. /*
  1070. * We need to toggle CS state for OMAP take this
  1071. * change in account.
  1072. */
  1073. MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
  1074. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1075. MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
  1076. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1077. }
  1078. }
  1079. omap2_mcspi_disable_clocks(mcspi);
  1080. return 0;
  1081. }
  1082. #else
  1083. #define omap2_mcspi_resume NULL
  1084. #endif
  1085. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1086. .resume = omap2_mcspi_resume,
  1087. .runtime_resume = omap_mcspi_runtime_resume,
  1088. };
  1089. static struct platform_driver omap2_mcspi_driver = {
  1090. .driver = {
  1091. .name = "omap2_mcspi",
  1092. .owner = THIS_MODULE,
  1093. .pm = &omap2_mcspi_pm_ops,
  1094. .of_match_table = omap_mcspi_of_match,
  1095. },
  1096. .probe = omap2_mcspi_probe,
  1097. .remove = __devexit_p(omap2_mcspi_remove),
  1098. };
  1099. module_platform_driver(omap2_mcspi_driver);
  1100. MODULE_LICENSE("GPL");