spi-omap2-mcspi.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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 = devm_kzalloc(&spi->dev , 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. }
  707. if (spi->chip_select < spi->master->num_chipselect) {
  708. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  709. if (mcspi_dma->dma_rx_channel != -1) {
  710. omap_free_dma(mcspi_dma->dma_rx_channel);
  711. mcspi_dma->dma_rx_channel = -1;
  712. }
  713. if (mcspi_dma->dma_tx_channel != -1) {
  714. omap_free_dma(mcspi_dma->dma_tx_channel);
  715. mcspi_dma->dma_tx_channel = -1;
  716. }
  717. }
  718. }
  719. static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
  720. {
  721. /* We only enable one channel at a time -- the one whose message is
  722. * -- although this controller would gladly
  723. * arbitrate among multiple channels. This corresponds to "single
  724. * channel" master mode. As a side effect, we need to manage the
  725. * chipselect with the FORCE bit ... CS != channel enable.
  726. */
  727. struct spi_device *spi;
  728. struct spi_transfer *t = NULL;
  729. int cs_active = 0;
  730. struct omap2_mcspi_cs *cs;
  731. struct omap2_mcspi_device_config *cd;
  732. int par_override = 0;
  733. int status = 0;
  734. u32 chconf;
  735. spi = m->spi;
  736. cs = spi->controller_state;
  737. cd = spi->controller_data;
  738. omap2_mcspi_set_enable(spi, 1);
  739. list_for_each_entry(t, &m->transfers, transfer_list) {
  740. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  741. status = -EINVAL;
  742. break;
  743. }
  744. if (par_override || t->speed_hz || t->bits_per_word) {
  745. par_override = 1;
  746. status = omap2_mcspi_setup_transfer(spi, t);
  747. if (status < 0)
  748. break;
  749. if (!t->speed_hz && !t->bits_per_word)
  750. par_override = 0;
  751. }
  752. if (!cs_active) {
  753. omap2_mcspi_force_cs(spi, 1);
  754. cs_active = 1;
  755. }
  756. chconf = mcspi_cached_chconf0(spi);
  757. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  758. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  759. if (t->tx_buf == NULL)
  760. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  761. else if (t->rx_buf == NULL)
  762. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  763. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  764. /* Turbo mode is for more than one word */
  765. if (t->len > ((cs->word_len + 7) >> 3))
  766. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  767. }
  768. mcspi_write_chconf0(spi, chconf);
  769. if (t->len) {
  770. unsigned count;
  771. /* RX_ONLY mode needs dummy data in TX reg */
  772. if (t->tx_buf == NULL)
  773. __raw_writel(0, cs->base
  774. + OMAP2_MCSPI_TX0);
  775. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  776. count = omap2_mcspi_txrx_dma(spi, t);
  777. else
  778. count = omap2_mcspi_txrx_pio(spi, t);
  779. m->actual_length += count;
  780. if (count != t->len) {
  781. status = -EIO;
  782. break;
  783. }
  784. }
  785. if (t->delay_usecs)
  786. udelay(t->delay_usecs);
  787. /* ignore the "leave it on after last xfer" hint */
  788. if (t->cs_change) {
  789. omap2_mcspi_force_cs(spi, 0);
  790. cs_active = 0;
  791. }
  792. }
  793. /* Restore defaults if they were overriden */
  794. if (par_override) {
  795. par_override = 0;
  796. status = omap2_mcspi_setup_transfer(spi, NULL);
  797. }
  798. if (cs_active)
  799. omap2_mcspi_force_cs(spi, 0);
  800. omap2_mcspi_set_enable(spi, 0);
  801. m->status = status;
  802. }
  803. static int omap2_mcspi_transfer_one_message(struct spi_master *master,
  804. struct spi_message *m)
  805. {
  806. struct omap2_mcspi *mcspi;
  807. struct spi_transfer *t;
  808. mcspi = spi_master_get_devdata(master);
  809. m->actual_length = 0;
  810. m->status = 0;
  811. /* reject invalid messages and transfers */
  812. if (list_empty(&m->transfers))
  813. return -EINVAL;
  814. list_for_each_entry(t, &m->transfers, transfer_list) {
  815. const void *tx_buf = t->tx_buf;
  816. void *rx_buf = t->rx_buf;
  817. unsigned len = t->len;
  818. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  819. || (len && !(rx_buf || tx_buf))
  820. || (t->bits_per_word &&
  821. ( t->bits_per_word < 4
  822. || t->bits_per_word > 32))) {
  823. dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  824. t->speed_hz,
  825. len,
  826. tx_buf ? "tx" : "",
  827. rx_buf ? "rx" : "",
  828. t->bits_per_word);
  829. return -EINVAL;
  830. }
  831. if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
  832. dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
  833. t->speed_hz,
  834. OMAP2_MCSPI_MAX_FREQ >> 15);
  835. return -EINVAL;
  836. }
  837. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  838. continue;
  839. if (tx_buf != NULL) {
  840. t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
  841. len, DMA_TO_DEVICE);
  842. if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
  843. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  844. 'T', len);
  845. return -EINVAL;
  846. }
  847. }
  848. if (rx_buf != NULL) {
  849. t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
  850. DMA_FROM_DEVICE);
  851. if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
  852. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  853. 'R', len);
  854. if (tx_buf != NULL)
  855. dma_unmap_single(mcspi->dev, t->tx_dma,
  856. len, DMA_TO_DEVICE);
  857. return -EINVAL;
  858. }
  859. }
  860. }
  861. omap2_mcspi_work(mcspi, m);
  862. spi_finalize_current_message(master);
  863. return 0;
  864. }
  865. static int __init omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
  866. {
  867. struct spi_master *master = mcspi->master;
  868. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  869. int ret = 0;
  870. ret = omap2_mcspi_enable_clocks(mcspi);
  871. if (ret < 0)
  872. return ret;
  873. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
  874. OMAP2_MCSPI_WAKEUPENABLE_WKEN);
  875. ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  876. omap2_mcspi_set_master_mode(master);
  877. omap2_mcspi_disable_clocks(mcspi);
  878. return 0;
  879. }
  880. static int omap_mcspi_runtime_resume(struct device *dev)
  881. {
  882. struct omap2_mcspi *mcspi;
  883. struct spi_master *master;
  884. master = dev_get_drvdata(dev);
  885. mcspi = spi_master_get_devdata(master);
  886. omap2_mcspi_restore_ctx(mcspi);
  887. return 0;
  888. }
  889. static struct omap2_mcspi_platform_config omap2_pdata = {
  890. .regs_offset = 0,
  891. };
  892. static struct omap2_mcspi_platform_config omap4_pdata = {
  893. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  894. };
  895. static const struct of_device_id omap_mcspi_of_match[] = {
  896. {
  897. .compatible = "ti,omap2-mcspi",
  898. .data = &omap2_pdata,
  899. },
  900. {
  901. .compatible = "ti,omap4-mcspi",
  902. .data = &omap4_pdata,
  903. },
  904. { },
  905. };
  906. MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
  907. static int __devinit omap2_mcspi_probe(struct platform_device *pdev)
  908. {
  909. struct spi_master *master;
  910. struct omap2_mcspi_platform_config *pdata;
  911. struct omap2_mcspi *mcspi;
  912. struct resource *r;
  913. int status = 0, i;
  914. u32 regs_offset = 0;
  915. static int bus_num = 1;
  916. struct device_node *node = pdev->dev.of_node;
  917. const struct of_device_id *match;
  918. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  919. if (master == NULL) {
  920. dev_dbg(&pdev->dev, "master allocation failed\n");
  921. return -ENOMEM;
  922. }
  923. /* the spi->mode bits understood by this driver: */
  924. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  925. master->setup = omap2_mcspi_setup;
  926. master->prepare_transfer_hardware = omap2_prepare_transfer;
  927. master->unprepare_transfer_hardware = omap2_unprepare_transfer;
  928. master->transfer_one_message = omap2_mcspi_transfer_one_message;
  929. master->cleanup = omap2_mcspi_cleanup;
  930. master->dev.of_node = node;
  931. match = of_match_device(omap_mcspi_of_match, &pdev->dev);
  932. if (match) {
  933. u32 num_cs = 1; /* default number of chipselect */
  934. pdata = match->data;
  935. of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
  936. master->num_chipselect = num_cs;
  937. master->bus_num = bus_num++;
  938. } else {
  939. pdata = pdev->dev.platform_data;
  940. master->num_chipselect = pdata->num_cs;
  941. if (pdev->id != -1)
  942. master->bus_num = pdev->id;
  943. }
  944. regs_offset = pdata->regs_offset;
  945. dev_set_drvdata(&pdev->dev, master);
  946. mcspi = spi_master_get_devdata(master);
  947. mcspi->master = master;
  948. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  949. if (r == NULL) {
  950. status = -ENODEV;
  951. goto free_master;
  952. }
  953. r->start += regs_offset;
  954. r->end += regs_offset;
  955. mcspi->phys = r->start;
  956. mcspi->base = devm_request_and_ioremap(&pdev->dev, r);
  957. if (!mcspi->base) {
  958. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  959. status = -ENOMEM;
  960. goto free_master;
  961. }
  962. mcspi->dev = &pdev->dev;
  963. INIT_LIST_HEAD(&mcspi->ctx.cs);
  964. mcspi->dma_channels = kcalloc(master->num_chipselect,
  965. sizeof(struct omap2_mcspi_dma),
  966. GFP_KERNEL);
  967. if (mcspi->dma_channels == NULL)
  968. goto free_master;
  969. for (i = 0; i < master->num_chipselect; i++) {
  970. char dma_ch_name[14];
  971. struct resource *dma_res;
  972. sprintf(dma_ch_name, "rx%d", i);
  973. dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  974. dma_ch_name);
  975. if (!dma_res) {
  976. dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
  977. status = -ENODEV;
  978. break;
  979. }
  980. mcspi->dma_channels[i].dma_rx_channel = -1;
  981. mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
  982. sprintf(dma_ch_name, "tx%d", i);
  983. dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  984. dma_ch_name);
  985. if (!dma_res) {
  986. dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
  987. status = -ENODEV;
  988. break;
  989. }
  990. mcspi->dma_channels[i].dma_tx_channel = -1;
  991. mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
  992. }
  993. if (status < 0)
  994. goto dma_chnl_free;
  995. pm_runtime_use_autosuspend(&pdev->dev);
  996. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  997. pm_runtime_enable(&pdev->dev);
  998. if (status || omap2_mcspi_master_setup(mcspi) < 0)
  999. goto disable_pm;
  1000. status = spi_register_master(master);
  1001. if (status < 0)
  1002. goto err_spi_register;
  1003. return status;
  1004. err_spi_register:
  1005. spi_master_put(master);
  1006. disable_pm:
  1007. pm_runtime_disable(&pdev->dev);
  1008. dma_chnl_free:
  1009. kfree(mcspi->dma_channels);
  1010. free_master:
  1011. kfree(master);
  1012. platform_set_drvdata(pdev, NULL);
  1013. return status;
  1014. }
  1015. static int __devexit omap2_mcspi_remove(struct platform_device *pdev)
  1016. {
  1017. struct spi_master *master;
  1018. struct omap2_mcspi *mcspi;
  1019. struct omap2_mcspi_dma *dma_channels;
  1020. master = dev_get_drvdata(&pdev->dev);
  1021. mcspi = spi_master_get_devdata(master);
  1022. dma_channels = mcspi->dma_channels;
  1023. omap2_mcspi_disable_clocks(mcspi);
  1024. pm_runtime_disable(&pdev->dev);
  1025. spi_unregister_master(master);
  1026. kfree(dma_channels);
  1027. platform_set_drvdata(pdev, NULL);
  1028. return 0;
  1029. }
  1030. /* work with hotplug and coldplug */
  1031. MODULE_ALIAS("platform:omap2_mcspi");
  1032. #ifdef CONFIG_SUSPEND
  1033. /*
  1034. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1035. * unactive state when driver was suspend, then force it to unactive state at
  1036. * wake up.
  1037. */
  1038. static int omap2_mcspi_resume(struct device *dev)
  1039. {
  1040. struct spi_master *master = dev_get_drvdata(dev);
  1041. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1042. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1043. struct omap2_mcspi_cs *cs;
  1044. omap2_mcspi_enable_clocks(mcspi);
  1045. list_for_each_entry(cs, &ctx->cs, node) {
  1046. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1047. /*
  1048. * We need to toggle CS state for OMAP take this
  1049. * change in account.
  1050. */
  1051. MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
  1052. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1053. MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
  1054. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1055. }
  1056. }
  1057. omap2_mcspi_disable_clocks(mcspi);
  1058. return 0;
  1059. }
  1060. #else
  1061. #define omap2_mcspi_resume NULL
  1062. #endif
  1063. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1064. .resume = omap2_mcspi_resume,
  1065. .runtime_resume = omap_mcspi_runtime_resume,
  1066. };
  1067. static struct platform_driver omap2_mcspi_driver = {
  1068. .driver = {
  1069. .name = "omap2_mcspi",
  1070. .owner = THIS_MODULE,
  1071. .pm = &omap2_mcspi_pm_ops,
  1072. .of_match_table = omap_mcspi_of_match,
  1073. },
  1074. .probe = omap2_mcspi_probe,
  1075. .remove = __devexit_p(omap2_mcspi_remove),
  1076. };
  1077. module_platform_driver(omap2_mcspi_driver);
  1078. MODULE_LICENSE("GPL");