spi-tegra20-slink.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /*
  2. * SPI driver for Nvidia's Tegra20/Tegra30 SLINK Controller.
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/dmapool.h>
  24. #include <linux/err.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/kernel.h>
  28. #include <linux/kthread.h>
  29. #include <linux/module.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/reset.h>
  35. #include <linux/spi/spi.h>
  36. #define SLINK_COMMAND 0x000
  37. #define SLINK_BIT_LENGTH(x) (((x) & 0x1f) << 0)
  38. #define SLINK_WORD_SIZE(x) (((x) & 0x1f) << 5)
  39. #define SLINK_BOTH_EN (1 << 10)
  40. #define SLINK_CS_SW (1 << 11)
  41. #define SLINK_CS_VALUE (1 << 12)
  42. #define SLINK_CS_POLARITY (1 << 13)
  43. #define SLINK_IDLE_SDA_DRIVE_LOW (0 << 16)
  44. #define SLINK_IDLE_SDA_DRIVE_HIGH (1 << 16)
  45. #define SLINK_IDLE_SDA_PULL_LOW (2 << 16)
  46. #define SLINK_IDLE_SDA_PULL_HIGH (3 << 16)
  47. #define SLINK_IDLE_SDA_MASK (3 << 16)
  48. #define SLINK_CS_POLARITY1 (1 << 20)
  49. #define SLINK_CK_SDA (1 << 21)
  50. #define SLINK_CS_POLARITY2 (1 << 22)
  51. #define SLINK_CS_POLARITY3 (1 << 23)
  52. #define SLINK_IDLE_SCLK_DRIVE_LOW (0 << 24)
  53. #define SLINK_IDLE_SCLK_DRIVE_HIGH (1 << 24)
  54. #define SLINK_IDLE_SCLK_PULL_LOW (2 << 24)
  55. #define SLINK_IDLE_SCLK_PULL_HIGH (3 << 24)
  56. #define SLINK_IDLE_SCLK_MASK (3 << 24)
  57. #define SLINK_M_S (1 << 28)
  58. #define SLINK_WAIT (1 << 29)
  59. #define SLINK_GO (1 << 30)
  60. #define SLINK_ENB (1 << 31)
  61. #define SLINK_MODES (SLINK_IDLE_SCLK_MASK | SLINK_CK_SDA)
  62. #define SLINK_COMMAND2 0x004
  63. #define SLINK_LSBFE (1 << 0)
  64. #define SLINK_SSOE (1 << 1)
  65. #define SLINK_SPIE (1 << 4)
  66. #define SLINK_BIDIROE (1 << 6)
  67. #define SLINK_MODFEN (1 << 7)
  68. #define SLINK_INT_SIZE(x) (((x) & 0x1f) << 8)
  69. #define SLINK_CS_ACTIVE_BETWEEN (1 << 17)
  70. #define SLINK_SS_EN_CS(x) (((x) & 0x3) << 18)
  71. #define SLINK_SS_SETUP(x) (((x) & 0x3) << 20)
  72. #define SLINK_FIFO_REFILLS_0 (0 << 22)
  73. #define SLINK_FIFO_REFILLS_1 (1 << 22)
  74. #define SLINK_FIFO_REFILLS_2 (2 << 22)
  75. #define SLINK_FIFO_REFILLS_3 (3 << 22)
  76. #define SLINK_FIFO_REFILLS_MASK (3 << 22)
  77. #define SLINK_WAIT_PACK_INT(x) (((x) & 0x7) << 26)
  78. #define SLINK_SPC0 (1 << 29)
  79. #define SLINK_TXEN (1 << 30)
  80. #define SLINK_RXEN (1 << 31)
  81. #define SLINK_STATUS 0x008
  82. #define SLINK_COUNT(val) (((val) >> 0) & 0x1f)
  83. #define SLINK_WORD(val) (((val) >> 5) & 0x1f)
  84. #define SLINK_BLK_CNT(val) (((val) >> 0) & 0xffff)
  85. #define SLINK_MODF (1 << 16)
  86. #define SLINK_RX_UNF (1 << 18)
  87. #define SLINK_TX_OVF (1 << 19)
  88. #define SLINK_TX_FULL (1 << 20)
  89. #define SLINK_TX_EMPTY (1 << 21)
  90. #define SLINK_RX_FULL (1 << 22)
  91. #define SLINK_RX_EMPTY (1 << 23)
  92. #define SLINK_TX_UNF (1 << 24)
  93. #define SLINK_RX_OVF (1 << 25)
  94. #define SLINK_TX_FLUSH (1 << 26)
  95. #define SLINK_RX_FLUSH (1 << 27)
  96. #define SLINK_SCLK (1 << 28)
  97. #define SLINK_ERR (1 << 29)
  98. #define SLINK_RDY (1 << 30)
  99. #define SLINK_BSY (1 << 31)
  100. #define SLINK_FIFO_ERROR (SLINK_TX_OVF | SLINK_RX_UNF | \
  101. SLINK_TX_UNF | SLINK_RX_OVF)
  102. #define SLINK_FIFO_EMPTY (SLINK_TX_EMPTY | SLINK_RX_EMPTY)
  103. #define SLINK_MAS_DATA 0x010
  104. #define SLINK_SLAVE_DATA 0x014
  105. #define SLINK_DMA_CTL 0x018
  106. #define SLINK_DMA_BLOCK_SIZE(x) (((x) & 0xffff) << 0)
  107. #define SLINK_TX_TRIG_1 (0 << 16)
  108. #define SLINK_TX_TRIG_4 (1 << 16)
  109. #define SLINK_TX_TRIG_8 (2 << 16)
  110. #define SLINK_TX_TRIG_16 (3 << 16)
  111. #define SLINK_TX_TRIG_MASK (3 << 16)
  112. #define SLINK_RX_TRIG_1 (0 << 18)
  113. #define SLINK_RX_TRIG_4 (1 << 18)
  114. #define SLINK_RX_TRIG_8 (2 << 18)
  115. #define SLINK_RX_TRIG_16 (3 << 18)
  116. #define SLINK_RX_TRIG_MASK (3 << 18)
  117. #define SLINK_PACKED (1 << 20)
  118. #define SLINK_PACK_SIZE_4 (0 << 21)
  119. #define SLINK_PACK_SIZE_8 (1 << 21)
  120. #define SLINK_PACK_SIZE_16 (2 << 21)
  121. #define SLINK_PACK_SIZE_32 (3 << 21)
  122. #define SLINK_PACK_SIZE_MASK (3 << 21)
  123. #define SLINK_IE_TXC (1 << 26)
  124. #define SLINK_IE_RXC (1 << 27)
  125. #define SLINK_DMA_EN (1 << 31)
  126. #define SLINK_STATUS2 0x01c
  127. #define SLINK_TX_FIFO_EMPTY_COUNT(val) (((val) & 0x3f) >> 0)
  128. #define SLINK_RX_FIFO_FULL_COUNT(val) (((val) & 0x3f0000) >> 16)
  129. #define SLINK_SS_HOLD_TIME(val) (((val) & 0xF) << 6)
  130. #define SLINK_TX_FIFO 0x100
  131. #define SLINK_RX_FIFO 0x180
  132. #define DATA_DIR_TX (1 << 0)
  133. #define DATA_DIR_RX (1 << 1)
  134. #define SLINK_DMA_TIMEOUT (msecs_to_jiffies(1000))
  135. #define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
  136. #define TX_FIFO_EMPTY_COUNT_MAX SLINK_TX_FIFO_EMPTY_COUNT(0x20)
  137. #define RX_FIFO_FULL_COUNT_ZERO SLINK_RX_FIFO_FULL_COUNT(0)
  138. #define SLINK_STATUS2_RESET \
  139. (TX_FIFO_EMPTY_COUNT_MAX | RX_FIFO_FULL_COUNT_ZERO << 16)
  140. #define MAX_CHIP_SELECT 4
  141. #define SLINK_FIFO_DEPTH 32
  142. struct tegra_slink_chip_data {
  143. bool cs_hold_time;
  144. };
  145. struct tegra_slink_data {
  146. struct device *dev;
  147. struct spi_master *master;
  148. const struct tegra_slink_chip_data *chip_data;
  149. spinlock_t lock;
  150. struct clk *clk;
  151. struct reset_control *rst;
  152. void __iomem *base;
  153. phys_addr_t phys;
  154. unsigned irq;
  155. u32 cur_speed;
  156. struct spi_device *cur_spi;
  157. unsigned cur_pos;
  158. unsigned cur_len;
  159. unsigned words_per_32bit;
  160. unsigned bytes_per_word;
  161. unsigned curr_dma_words;
  162. unsigned cur_direction;
  163. unsigned cur_rx_pos;
  164. unsigned cur_tx_pos;
  165. unsigned dma_buf_size;
  166. unsigned max_buf_size;
  167. bool is_curr_dma_xfer;
  168. struct completion rx_dma_complete;
  169. struct completion tx_dma_complete;
  170. u32 tx_status;
  171. u32 rx_status;
  172. u32 status_reg;
  173. bool is_packed;
  174. u32 packed_size;
  175. u32 command_reg;
  176. u32 command2_reg;
  177. u32 dma_control_reg;
  178. u32 def_command_reg;
  179. u32 def_command2_reg;
  180. struct completion xfer_completion;
  181. struct spi_transfer *curr_xfer;
  182. struct dma_chan *rx_dma_chan;
  183. u32 *rx_dma_buf;
  184. dma_addr_t rx_dma_phys;
  185. struct dma_async_tx_descriptor *rx_dma_desc;
  186. struct dma_chan *tx_dma_chan;
  187. u32 *tx_dma_buf;
  188. dma_addr_t tx_dma_phys;
  189. struct dma_async_tx_descriptor *tx_dma_desc;
  190. };
  191. static int tegra_slink_runtime_suspend(struct device *dev);
  192. static int tegra_slink_runtime_resume(struct device *dev);
  193. static inline u32 tegra_slink_readl(struct tegra_slink_data *tspi,
  194. unsigned long reg)
  195. {
  196. return readl(tspi->base + reg);
  197. }
  198. static inline void tegra_slink_writel(struct tegra_slink_data *tspi,
  199. u32 val, unsigned long reg)
  200. {
  201. writel(val, tspi->base + reg);
  202. /* Read back register to make sure that register writes completed */
  203. if (reg != SLINK_TX_FIFO)
  204. readl(tspi->base + SLINK_MAS_DATA);
  205. }
  206. static void tegra_slink_clear_status(struct tegra_slink_data *tspi)
  207. {
  208. u32 val_write;
  209. tegra_slink_readl(tspi, SLINK_STATUS);
  210. /* Write 1 to clear status register */
  211. val_write = SLINK_RDY | SLINK_FIFO_ERROR;
  212. tegra_slink_writel(tspi, val_write, SLINK_STATUS);
  213. }
  214. static u32 tegra_slink_get_packed_size(struct tegra_slink_data *tspi,
  215. struct spi_transfer *t)
  216. {
  217. switch (tspi->bytes_per_word) {
  218. case 0:
  219. return SLINK_PACK_SIZE_4;
  220. case 1:
  221. return SLINK_PACK_SIZE_8;
  222. case 2:
  223. return SLINK_PACK_SIZE_16;
  224. case 4:
  225. return SLINK_PACK_SIZE_32;
  226. default:
  227. return 0;
  228. }
  229. }
  230. static unsigned tegra_slink_calculate_curr_xfer_param(
  231. struct spi_device *spi, struct tegra_slink_data *tspi,
  232. struct spi_transfer *t)
  233. {
  234. unsigned remain_len = t->len - tspi->cur_pos;
  235. unsigned max_word;
  236. unsigned bits_per_word;
  237. unsigned max_len;
  238. unsigned total_fifo_words;
  239. bits_per_word = t->bits_per_word;
  240. tspi->bytes_per_word = DIV_ROUND_UP(bits_per_word, 8);
  241. if (bits_per_word == 8 || bits_per_word == 16) {
  242. tspi->is_packed = 1;
  243. tspi->words_per_32bit = 32/bits_per_word;
  244. } else {
  245. tspi->is_packed = 0;
  246. tspi->words_per_32bit = 1;
  247. }
  248. tspi->packed_size = tegra_slink_get_packed_size(tspi, t);
  249. if (tspi->is_packed) {
  250. max_len = min(remain_len, tspi->max_buf_size);
  251. tspi->curr_dma_words = max_len/tspi->bytes_per_word;
  252. total_fifo_words = max_len/4;
  253. } else {
  254. max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
  255. max_word = min(max_word, tspi->max_buf_size/4);
  256. tspi->curr_dma_words = max_word;
  257. total_fifo_words = max_word;
  258. }
  259. return total_fifo_words;
  260. }
  261. static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(
  262. struct tegra_slink_data *tspi, struct spi_transfer *t)
  263. {
  264. unsigned nbytes;
  265. unsigned tx_empty_count;
  266. u32 fifo_status;
  267. unsigned max_n_32bit;
  268. unsigned i, count;
  269. unsigned int written_words;
  270. unsigned fifo_words_left;
  271. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  272. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  273. tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);
  274. if (tspi->is_packed) {
  275. fifo_words_left = tx_empty_count * tspi->words_per_32bit;
  276. written_words = min(fifo_words_left, tspi->curr_dma_words);
  277. nbytes = written_words * tspi->bytes_per_word;
  278. max_n_32bit = DIV_ROUND_UP(nbytes, 4);
  279. for (count = 0; count < max_n_32bit; count++) {
  280. u32 x = 0;
  281. for (i = 0; (i < 4) && nbytes; i++, nbytes--)
  282. x |= (u32)(*tx_buf++) << (i * 8);
  283. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  284. }
  285. } else {
  286. max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
  287. written_words = max_n_32bit;
  288. nbytes = written_words * tspi->bytes_per_word;
  289. for (count = 0; count < max_n_32bit; count++) {
  290. u32 x = 0;
  291. for (i = 0; nbytes && (i < tspi->bytes_per_word);
  292. i++, nbytes--)
  293. x |= (u32)(*tx_buf++) << (i * 8);
  294. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  295. }
  296. }
  297. tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
  298. return written_words;
  299. }
  300. static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(
  301. struct tegra_slink_data *tspi, struct spi_transfer *t)
  302. {
  303. unsigned rx_full_count;
  304. u32 fifo_status;
  305. unsigned i, count;
  306. unsigned int read_words = 0;
  307. unsigned len;
  308. u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
  309. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  310. rx_full_count = SLINK_RX_FIFO_FULL_COUNT(fifo_status);
  311. if (tspi->is_packed) {
  312. len = tspi->curr_dma_words * tspi->bytes_per_word;
  313. for (count = 0; count < rx_full_count; count++) {
  314. u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  315. for (i = 0; len && (i < 4); i++, len--)
  316. *rx_buf++ = (x >> i*8) & 0xFF;
  317. }
  318. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  319. read_words += tspi->curr_dma_words;
  320. } else {
  321. for (count = 0; count < rx_full_count; count++) {
  322. u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  323. for (i = 0; (i < tspi->bytes_per_word); i++)
  324. *rx_buf++ = (x >> (i*8)) & 0xFF;
  325. }
  326. tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
  327. read_words += rx_full_count;
  328. }
  329. return read_words;
  330. }
  331. static void tegra_slink_copy_client_txbuf_to_spi_txbuf(
  332. struct tegra_slink_data *tspi, struct spi_transfer *t)
  333. {
  334. /* Make the dma buffer to read by cpu */
  335. dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
  336. tspi->dma_buf_size, DMA_TO_DEVICE);
  337. if (tspi->is_packed) {
  338. unsigned len = tspi->curr_dma_words * tspi->bytes_per_word;
  339. memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
  340. } else {
  341. unsigned int i;
  342. unsigned int count;
  343. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  344. unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
  345. for (count = 0; count < tspi->curr_dma_words; count++) {
  346. u32 x = 0;
  347. for (i = 0; consume && (i < tspi->bytes_per_word);
  348. i++, consume--)
  349. x |= (u32)(*tx_buf++) << (i * 8);
  350. tspi->tx_dma_buf[count] = x;
  351. }
  352. }
  353. tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  354. /* Make the dma buffer to read by dma */
  355. dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
  356. tspi->dma_buf_size, DMA_TO_DEVICE);
  357. }
  358. static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(
  359. struct tegra_slink_data *tspi, struct spi_transfer *t)
  360. {
  361. unsigned len;
  362. /* Make the dma buffer to read by cpu */
  363. dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
  364. tspi->dma_buf_size, DMA_FROM_DEVICE);
  365. if (tspi->is_packed) {
  366. len = tspi->curr_dma_words * tspi->bytes_per_word;
  367. memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
  368. } else {
  369. unsigned int i;
  370. unsigned int count;
  371. unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
  372. u32 rx_mask = ((u32)1 << t->bits_per_word) - 1;
  373. for (count = 0; count < tspi->curr_dma_words; count++) {
  374. u32 x = tspi->rx_dma_buf[count] & rx_mask;
  375. for (i = 0; (i < tspi->bytes_per_word); i++)
  376. *rx_buf++ = (x >> (i*8)) & 0xFF;
  377. }
  378. }
  379. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  380. /* Make the dma buffer to read by dma */
  381. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  382. tspi->dma_buf_size, DMA_FROM_DEVICE);
  383. }
  384. static void tegra_slink_dma_complete(void *args)
  385. {
  386. struct completion *dma_complete = args;
  387. complete(dma_complete);
  388. }
  389. static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)
  390. {
  391. reinit_completion(&tspi->tx_dma_complete);
  392. tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
  393. tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
  394. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  395. if (!tspi->tx_dma_desc) {
  396. dev_err(tspi->dev, "Not able to get desc for Tx\n");
  397. return -EIO;
  398. }
  399. tspi->tx_dma_desc->callback = tegra_slink_dma_complete;
  400. tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
  401. dmaengine_submit(tspi->tx_dma_desc);
  402. dma_async_issue_pending(tspi->tx_dma_chan);
  403. return 0;
  404. }
  405. static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)
  406. {
  407. reinit_completion(&tspi->rx_dma_complete);
  408. tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
  409. tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
  410. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  411. if (!tspi->rx_dma_desc) {
  412. dev_err(tspi->dev, "Not able to get desc for Rx\n");
  413. return -EIO;
  414. }
  415. tspi->rx_dma_desc->callback = tegra_slink_dma_complete;
  416. tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
  417. dmaengine_submit(tspi->rx_dma_desc);
  418. dma_async_issue_pending(tspi->rx_dma_chan);
  419. return 0;
  420. }
  421. static int tegra_slink_start_dma_based_transfer(
  422. struct tegra_slink_data *tspi, struct spi_transfer *t)
  423. {
  424. u32 val;
  425. unsigned int len;
  426. int ret = 0;
  427. u32 status;
  428. /* Make sure that Rx and Tx fifo are empty */
  429. status = tegra_slink_readl(tspi, SLINK_STATUS);
  430. if ((status & SLINK_FIFO_EMPTY) != SLINK_FIFO_EMPTY) {
  431. dev_err(tspi->dev, "Rx/Tx fifo are not empty status 0x%08x\n",
  432. (unsigned)status);
  433. return -EIO;
  434. }
  435. val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);
  436. val |= tspi->packed_size;
  437. if (tspi->is_packed)
  438. len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
  439. 4) * 4;
  440. else
  441. len = tspi->curr_dma_words * 4;
  442. /* Set attention level based on length of transfer */
  443. if (len & 0xF)
  444. val |= SLINK_TX_TRIG_1 | SLINK_RX_TRIG_1;
  445. else if (((len) >> 4) & 0x1)
  446. val |= SLINK_TX_TRIG_4 | SLINK_RX_TRIG_4;
  447. else
  448. val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;
  449. if (tspi->cur_direction & DATA_DIR_TX)
  450. val |= SLINK_IE_TXC;
  451. if (tspi->cur_direction & DATA_DIR_RX)
  452. val |= SLINK_IE_RXC;
  453. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  454. tspi->dma_control_reg = val;
  455. if (tspi->cur_direction & DATA_DIR_TX) {
  456. tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);
  457. wmb();
  458. ret = tegra_slink_start_tx_dma(tspi, len);
  459. if (ret < 0) {
  460. dev_err(tspi->dev,
  461. "Starting tx dma failed, err %d\n", ret);
  462. return ret;
  463. }
  464. /* Wait for tx fifo to be fill before starting slink */
  465. status = tegra_slink_readl(tspi, SLINK_STATUS);
  466. while (!(status & SLINK_TX_FULL))
  467. status = tegra_slink_readl(tspi, SLINK_STATUS);
  468. }
  469. if (tspi->cur_direction & DATA_DIR_RX) {
  470. /* Make the dma buffer to read by dma */
  471. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  472. tspi->dma_buf_size, DMA_FROM_DEVICE);
  473. ret = tegra_slink_start_rx_dma(tspi, len);
  474. if (ret < 0) {
  475. dev_err(tspi->dev,
  476. "Starting rx dma failed, err %d\n", ret);
  477. if (tspi->cur_direction & DATA_DIR_TX)
  478. dmaengine_terminate_all(tspi->tx_dma_chan);
  479. return ret;
  480. }
  481. }
  482. tspi->is_curr_dma_xfer = true;
  483. if (tspi->is_packed) {
  484. val |= SLINK_PACKED;
  485. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  486. /* HW need small delay after settign Packed mode */
  487. udelay(1);
  488. }
  489. tspi->dma_control_reg = val;
  490. val |= SLINK_DMA_EN;
  491. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  492. return ret;
  493. }
  494. static int tegra_slink_start_cpu_based_transfer(
  495. struct tegra_slink_data *tspi, struct spi_transfer *t)
  496. {
  497. u32 val;
  498. unsigned cur_words;
  499. val = tspi->packed_size;
  500. if (tspi->cur_direction & DATA_DIR_TX)
  501. val |= SLINK_IE_TXC;
  502. if (tspi->cur_direction & DATA_DIR_RX)
  503. val |= SLINK_IE_RXC;
  504. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  505. tspi->dma_control_reg = val;
  506. if (tspi->cur_direction & DATA_DIR_TX)
  507. cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);
  508. else
  509. cur_words = tspi->curr_dma_words;
  510. val |= SLINK_DMA_BLOCK_SIZE(cur_words - 1);
  511. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  512. tspi->dma_control_reg = val;
  513. tspi->is_curr_dma_xfer = false;
  514. if (tspi->is_packed) {
  515. val |= SLINK_PACKED;
  516. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  517. udelay(1);
  518. wmb();
  519. }
  520. tspi->dma_control_reg = val;
  521. val |= SLINK_DMA_EN;
  522. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  523. return 0;
  524. }
  525. static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,
  526. bool dma_to_memory)
  527. {
  528. struct dma_chan *dma_chan;
  529. u32 *dma_buf;
  530. dma_addr_t dma_phys;
  531. int ret;
  532. struct dma_slave_config dma_sconfig;
  533. dma_chan = dma_request_slave_channel_reason(tspi->dev,
  534. dma_to_memory ? "rx" : "tx");
  535. if (IS_ERR(dma_chan)) {
  536. ret = PTR_ERR(dma_chan);
  537. if (ret != -EPROBE_DEFER)
  538. dev_err(tspi->dev,
  539. "Dma channel is not available: %d\n", ret);
  540. return ret;
  541. }
  542. dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
  543. &dma_phys, GFP_KERNEL);
  544. if (!dma_buf) {
  545. dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
  546. dma_release_channel(dma_chan);
  547. return -ENOMEM;
  548. }
  549. if (dma_to_memory) {
  550. dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
  551. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  552. dma_sconfig.src_maxburst = 0;
  553. } else {
  554. dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;
  555. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  556. dma_sconfig.dst_maxburst = 0;
  557. }
  558. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  559. if (ret)
  560. goto scrub;
  561. if (dma_to_memory) {
  562. tspi->rx_dma_chan = dma_chan;
  563. tspi->rx_dma_buf = dma_buf;
  564. tspi->rx_dma_phys = dma_phys;
  565. } else {
  566. tspi->tx_dma_chan = dma_chan;
  567. tspi->tx_dma_buf = dma_buf;
  568. tspi->tx_dma_phys = dma_phys;
  569. }
  570. return 0;
  571. scrub:
  572. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  573. dma_release_channel(dma_chan);
  574. return ret;
  575. }
  576. static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,
  577. bool dma_to_memory)
  578. {
  579. u32 *dma_buf;
  580. dma_addr_t dma_phys;
  581. struct dma_chan *dma_chan;
  582. if (dma_to_memory) {
  583. dma_buf = tspi->rx_dma_buf;
  584. dma_chan = tspi->rx_dma_chan;
  585. dma_phys = tspi->rx_dma_phys;
  586. tspi->rx_dma_chan = NULL;
  587. tspi->rx_dma_buf = NULL;
  588. } else {
  589. dma_buf = tspi->tx_dma_buf;
  590. dma_chan = tspi->tx_dma_chan;
  591. dma_phys = tspi->tx_dma_phys;
  592. tspi->tx_dma_buf = NULL;
  593. tspi->tx_dma_chan = NULL;
  594. }
  595. if (!dma_chan)
  596. return;
  597. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  598. dma_release_channel(dma_chan);
  599. }
  600. static int tegra_slink_start_transfer_one(struct spi_device *spi,
  601. struct spi_transfer *t)
  602. {
  603. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  604. u32 speed;
  605. u8 bits_per_word;
  606. unsigned total_fifo_words;
  607. int ret;
  608. u32 command;
  609. u32 command2;
  610. bits_per_word = t->bits_per_word;
  611. speed = t->speed_hz;
  612. if (speed != tspi->cur_speed) {
  613. clk_set_rate(tspi->clk, speed * 4);
  614. tspi->cur_speed = speed;
  615. }
  616. tspi->cur_spi = spi;
  617. tspi->cur_pos = 0;
  618. tspi->cur_rx_pos = 0;
  619. tspi->cur_tx_pos = 0;
  620. tspi->curr_xfer = t;
  621. total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
  622. command = tspi->command_reg;
  623. command &= ~SLINK_BIT_LENGTH(~0);
  624. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  625. command2 = tspi->command2_reg;
  626. command2 &= ~(SLINK_RXEN | SLINK_TXEN);
  627. tegra_slink_writel(tspi, command, SLINK_COMMAND);
  628. tspi->command_reg = command;
  629. tspi->cur_direction = 0;
  630. if (t->rx_buf) {
  631. command2 |= SLINK_RXEN;
  632. tspi->cur_direction |= DATA_DIR_RX;
  633. }
  634. if (t->tx_buf) {
  635. command2 |= SLINK_TXEN;
  636. tspi->cur_direction |= DATA_DIR_TX;
  637. }
  638. tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
  639. tspi->command2_reg = command2;
  640. if (total_fifo_words > SLINK_FIFO_DEPTH)
  641. ret = tegra_slink_start_dma_based_transfer(tspi, t);
  642. else
  643. ret = tegra_slink_start_cpu_based_transfer(tspi, t);
  644. return ret;
  645. }
  646. static int tegra_slink_setup(struct spi_device *spi)
  647. {
  648. static const u32 cs_pol_bit[MAX_CHIP_SELECT] = {
  649. SLINK_CS_POLARITY,
  650. SLINK_CS_POLARITY1,
  651. SLINK_CS_POLARITY2,
  652. SLINK_CS_POLARITY3,
  653. };
  654. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  655. u32 val;
  656. unsigned long flags;
  657. int ret;
  658. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  659. spi->bits_per_word,
  660. spi->mode & SPI_CPOL ? "" : "~",
  661. spi->mode & SPI_CPHA ? "" : "~",
  662. spi->max_speed_hz);
  663. ret = pm_runtime_get_sync(tspi->dev);
  664. if (ret < 0) {
  665. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  666. return ret;
  667. }
  668. spin_lock_irqsave(&tspi->lock, flags);
  669. val = tspi->def_command_reg;
  670. if (spi->mode & SPI_CS_HIGH)
  671. val |= cs_pol_bit[spi->chip_select];
  672. else
  673. val &= ~cs_pol_bit[spi->chip_select];
  674. tspi->def_command_reg = val;
  675. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  676. spin_unlock_irqrestore(&tspi->lock, flags);
  677. pm_runtime_put(tspi->dev);
  678. return 0;
  679. }
  680. static int tegra_slink_prepare_message(struct spi_master *master,
  681. struct spi_message *msg)
  682. {
  683. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  684. struct spi_device *spi = msg->spi;
  685. tegra_slink_clear_status(tspi);
  686. tspi->command_reg = tspi->def_command_reg;
  687. tspi->command_reg |= SLINK_CS_SW | SLINK_CS_VALUE;
  688. tspi->command2_reg = tspi->def_command2_reg;
  689. tspi->command2_reg |= SLINK_SS_EN_CS(spi->chip_select);
  690. tspi->command_reg &= ~SLINK_MODES;
  691. if (spi->mode & SPI_CPHA)
  692. tspi->command_reg |= SLINK_CK_SDA;
  693. if (spi->mode & SPI_CPOL)
  694. tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_HIGH;
  695. else
  696. tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_LOW;
  697. return 0;
  698. }
  699. static int tegra_slink_transfer_one(struct spi_master *master,
  700. struct spi_device *spi,
  701. struct spi_transfer *xfer)
  702. {
  703. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  704. int ret;
  705. reinit_completion(&tspi->xfer_completion);
  706. ret = tegra_slink_start_transfer_one(spi, xfer);
  707. if (ret < 0) {
  708. dev_err(tspi->dev,
  709. "spi can not start transfer, err %d\n", ret);
  710. return ret;
  711. }
  712. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  713. SLINK_DMA_TIMEOUT);
  714. if (WARN_ON(ret == 0)) {
  715. dev_err(tspi->dev,
  716. "spi trasfer timeout, err %d\n", ret);
  717. return -EIO;
  718. }
  719. if (tspi->tx_status)
  720. return tspi->tx_status;
  721. if (tspi->rx_status)
  722. return tspi->rx_status;
  723. return 0;
  724. }
  725. static int tegra_slink_unprepare_message(struct spi_master *master,
  726. struct spi_message *msg)
  727. {
  728. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  729. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  730. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  731. return 0;
  732. }
  733. static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
  734. {
  735. struct spi_transfer *t = tspi->curr_xfer;
  736. unsigned long flags;
  737. spin_lock_irqsave(&tspi->lock, flags);
  738. if (tspi->tx_status || tspi->rx_status ||
  739. (tspi->status_reg & SLINK_BSY)) {
  740. dev_err(tspi->dev,
  741. "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
  742. dev_err(tspi->dev,
  743. "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  744. tspi->command2_reg, tspi->dma_control_reg);
  745. reset_control_assert(tspi->rst);
  746. udelay(2);
  747. reset_control_deassert(tspi->rst);
  748. complete(&tspi->xfer_completion);
  749. goto exit;
  750. }
  751. if (tspi->cur_direction & DATA_DIR_RX)
  752. tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
  753. if (tspi->cur_direction & DATA_DIR_TX)
  754. tspi->cur_pos = tspi->cur_tx_pos;
  755. else
  756. tspi->cur_pos = tspi->cur_rx_pos;
  757. if (tspi->cur_pos == t->len) {
  758. complete(&tspi->xfer_completion);
  759. goto exit;
  760. }
  761. tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  762. tegra_slink_start_cpu_based_transfer(tspi, t);
  763. exit:
  764. spin_unlock_irqrestore(&tspi->lock, flags);
  765. return IRQ_HANDLED;
  766. }
  767. static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
  768. {
  769. struct spi_transfer *t = tspi->curr_xfer;
  770. long wait_status;
  771. int err = 0;
  772. unsigned total_fifo_words;
  773. unsigned long flags;
  774. /* Abort dmas if any error */
  775. if (tspi->cur_direction & DATA_DIR_TX) {
  776. if (tspi->tx_status) {
  777. dmaengine_terminate_all(tspi->tx_dma_chan);
  778. err += 1;
  779. } else {
  780. wait_status = wait_for_completion_interruptible_timeout(
  781. &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
  782. if (wait_status <= 0) {
  783. dmaengine_terminate_all(tspi->tx_dma_chan);
  784. dev_err(tspi->dev, "TxDma Xfer failed\n");
  785. err += 1;
  786. }
  787. }
  788. }
  789. if (tspi->cur_direction & DATA_DIR_RX) {
  790. if (tspi->rx_status) {
  791. dmaengine_terminate_all(tspi->rx_dma_chan);
  792. err += 2;
  793. } else {
  794. wait_status = wait_for_completion_interruptible_timeout(
  795. &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
  796. if (wait_status <= 0) {
  797. dmaengine_terminate_all(tspi->rx_dma_chan);
  798. dev_err(tspi->dev, "RxDma Xfer failed\n");
  799. err += 2;
  800. }
  801. }
  802. }
  803. spin_lock_irqsave(&tspi->lock, flags);
  804. if (err) {
  805. dev_err(tspi->dev,
  806. "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
  807. dev_err(tspi->dev,
  808. "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  809. tspi->command2_reg, tspi->dma_control_reg);
  810. reset_control_assert(tspi->rst);
  811. udelay(2);
  812. reset_control_assert(tspi->rst);
  813. complete(&tspi->xfer_completion);
  814. spin_unlock_irqrestore(&tspi->lock, flags);
  815. return IRQ_HANDLED;
  816. }
  817. if (tspi->cur_direction & DATA_DIR_RX)
  818. tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  819. if (tspi->cur_direction & DATA_DIR_TX)
  820. tspi->cur_pos = tspi->cur_tx_pos;
  821. else
  822. tspi->cur_pos = tspi->cur_rx_pos;
  823. if (tspi->cur_pos == t->len) {
  824. complete(&tspi->xfer_completion);
  825. goto exit;
  826. }
  827. /* Continue transfer in current message */
  828. total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
  829. tspi, t);
  830. if (total_fifo_words > SLINK_FIFO_DEPTH)
  831. err = tegra_slink_start_dma_based_transfer(tspi, t);
  832. else
  833. err = tegra_slink_start_cpu_based_transfer(tspi, t);
  834. exit:
  835. spin_unlock_irqrestore(&tspi->lock, flags);
  836. return IRQ_HANDLED;
  837. }
  838. static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
  839. {
  840. struct tegra_slink_data *tspi = context_data;
  841. if (!tspi->is_curr_dma_xfer)
  842. return handle_cpu_based_xfer(tspi);
  843. return handle_dma_based_xfer(tspi);
  844. }
  845. static irqreturn_t tegra_slink_isr(int irq, void *context_data)
  846. {
  847. struct tegra_slink_data *tspi = context_data;
  848. tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
  849. if (tspi->cur_direction & DATA_DIR_TX)
  850. tspi->tx_status = tspi->status_reg &
  851. (SLINK_TX_OVF | SLINK_TX_UNF);
  852. if (tspi->cur_direction & DATA_DIR_RX)
  853. tspi->rx_status = tspi->status_reg &
  854. (SLINK_RX_OVF | SLINK_RX_UNF);
  855. tegra_slink_clear_status(tspi);
  856. return IRQ_WAKE_THREAD;
  857. }
  858. static const struct tegra_slink_chip_data tegra30_spi_cdata = {
  859. .cs_hold_time = true,
  860. };
  861. static const struct tegra_slink_chip_data tegra20_spi_cdata = {
  862. .cs_hold_time = false,
  863. };
  864. static const struct of_device_id tegra_slink_of_match[] = {
  865. { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
  866. { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
  867. {}
  868. };
  869. MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
  870. static int tegra_slink_probe(struct platform_device *pdev)
  871. {
  872. struct spi_master *master;
  873. struct tegra_slink_data *tspi;
  874. struct resource *r;
  875. int ret, spi_irq;
  876. const struct tegra_slink_chip_data *cdata = NULL;
  877. const struct of_device_id *match;
  878. match = of_match_device(tegra_slink_of_match, &pdev->dev);
  879. if (!match) {
  880. dev_err(&pdev->dev, "Error: No device match found\n");
  881. return -ENODEV;
  882. }
  883. cdata = match->data;
  884. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  885. if (!master) {
  886. dev_err(&pdev->dev, "master allocation failed\n");
  887. return -ENOMEM;
  888. }
  889. /* the spi->mode bits understood by this driver: */
  890. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  891. master->setup = tegra_slink_setup;
  892. master->prepare_message = tegra_slink_prepare_message;
  893. master->transfer_one = tegra_slink_transfer_one;
  894. master->unprepare_message = tegra_slink_unprepare_message;
  895. master->auto_runtime_pm = true;
  896. master->num_chipselect = MAX_CHIP_SELECT;
  897. platform_set_drvdata(pdev, master);
  898. tspi = spi_master_get_devdata(master);
  899. tspi->master = master;
  900. tspi->dev = &pdev->dev;
  901. tspi->chip_data = cdata;
  902. spin_lock_init(&tspi->lock);
  903. if (of_property_read_u32(tspi->dev->of_node, "spi-max-frequency",
  904. &master->max_speed_hz))
  905. master->max_speed_hz = 25000000; /* 25MHz */
  906. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  907. if (!r) {
  908. dev_err(&pdev->dev, "No IO memory resource\n");
  909. ret = -ENODEV;
  910. goto exit_free_master;
  911. }
  912. tspi->phys = r->start;
  913. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  914. if (IS_ERR(tspi->base)) {
  915. ret = PTR_ERR(tspi->base);
  916. goto exit_free_master;
  917. }
  918. /* disabled clock may cause interrupt storm upon request */
  919. tspi->clk = devm_clk_get(&pdev->dev, NULL);
  920. if (IS_ERR(tspi->clk)) {
  921. ret = PTR_ERR(tspi->clk);
  922. dev_err(&pdev->dev, "Can not get clock %d\n", ret);
  923. goto exit_free_master;
  924. }
  925. ret = clk_prepare(tspi->clk);
  926. if (ret < 0) {
  927. dev_err(&pdev->dev, "Clock prepare failed %d\n", ret);
  928. goto exit_free_master;
  929. }
  930. ret = clk_enable(tspi->clk);
  931. if (ret < 0) {
  932. dev_err(&pdev->dev, "Clock enable failed %d\n", ret);
  933. goto exit_free_master;
  934. }
  935. spi_irq = platform_get_irq(pdev, 0);
  936. tspi->irq = spi_irq;
  937. ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
  938. tegra_slink_isr_thread, IRQF_ONESHOT,
  939. dev_name(&pdev->dev), tspi);
  940. if (ret < 0) {
  941. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  942. tspi->irq);
  943. goto exit_clk_disable;
  944. }
  945. tspi->rst = devm_reset_control_get(&pdev->dev, "spi");
  946. if (IS_ERR(tspi->rst)) {
  947. dev_err(&pdev->dev, "can not get reset\n");
  948. ret = PTR_ERR(tspi->rst);
  949. goto exit_free_irq;
  950. }
  951. tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
  952. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  953. ret = tegra_slink_init_dma_param(tspi, true);
  954. if (ret < 0)
  955. goto exit_free_irq;
  956. ret = tegra_slink_init_dma_param(tspi, false);
  957. if (ret < 0)
  958. goto exit_rx_dma_free;
  959. tspi->max_buf_size = tspi->dma_buf_size;
  960. init_completion(&tspi->tx_dma_complete);
  961. init_completion(&tspi->rx_dma_complete);
  962. init_completion(&tspi->xfer_completion);
  963. pm_runtime_enable(&pdev->dev);
  964. if (!pm_runtime_enabled(&pdev->dev)) {
  965. ret = tegra_slink_runtime_resume(&pdev->dev);
  966. if (ret)
  967. goto exit_pm_disable;
  968. }
  969. ret = pm_runtime_get_sync(&pdev->dev);
  970. if (ret < 0) {
  971. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  972. goto exit_pm_disable;
  973. }
  974. tspi->def_command_reg = SLINK_M_S;
  975. tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
  976. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  977. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  978. pm_runtime_put(&pdev->dev);
  979. master->dev.of_node = pdev->dev.of_node;
  980. ret = devm_spi_register_master(&pdev->dev, master);
  981. if (ret < 0) {
  982. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  983. goto exit_pm_disable;
  984. }
  985. return ret;
  986. exit_pm_disable:
  987. pm_runtime_disable(&pdev->dev);
  988. if (!pm_runtime_status_suspended(&pdev->dev))
  989. tegra_slink_runtime_suspend(&pdev->dev);
  990. tegra_slink_deinit_dma_param(tspi, false);
  991. exit_rx_dma_free:
  992. tegra_slink_deinit_dma_param(tspi, true);
  993. exit_free_irq:
  994. free_irq(spi_irq, tspi);
  995. exit_clk_disable:
  996. clk_disable(tspi->clk);
  997. exit_free_master:
  998. spi_master_put(master);
  999. return ret;
  1000. }
  1001. static int tegra_slink_remove(struct platform_device *pdev)
  1002. {
  1003. struct spi_master *master = platform_get_drvdata(pdev);
  1004. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1005. free_irq(tspi->irq, tspi);
  1006. clk_disable(tspi->clk);
  1007. if (tspi->tx_dma_chan)
  1008. tegra_slink_deinit_dma_param(tspi, false);
  1009. if (tspi->rx_dma_chan)
  1010. tegra_slink_deinit_dma_param(tspi, true);
  1011. pm_runtime_disable(&pdev->dev);
  1012. if (!pm_runtime_status_suspended(&pdev->dev))
  1013. tegra_slink_runtime_suspend(&pdev->dev);
  1014. return 0;
  1015. }
  1016. #ifdef CONFIG_PM_SLEEP
  1017. static int tegra_slink_suspend(struct device *dev)
  1018. {
  1019. struct spi_master *master = dev_get_drvdata(dev);
  1020. return spi_master_suspend(master);
  1021. }
  1022. static int tegra_slink_resume(struct device *dev)
  1023. {
  1024. struct spi_master *master = dev_get_drvdata(dev);
  1025. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1026. int ret;
  1027. ret = pm_runtime_get_sync(dev);
  1028. if (ret < 0) {
  1029. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1030. return ret;
  1031. }
  1032. tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
  1033. tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
  1034. pm_runtime_put(dev);
  1035. return spi_master_resume(master);
  1036. }
  1037. #endif
  1038. static int tegra_slink_runtime_suspend(struct device *dev)
  1039. {
  1040. struct spi_master *master = dev_get_drvdata(dev);
  1041. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1042. /* Flush all write which are in PPSB queue by reading back */
  1043. tegra_slink_readl(tspi, SLINK_MAS_DATA);
  1044. clk_disable_unprepare(tspi->clk);
  1045. return 0;
  1046. }
  1047. static int tegra_slink_runtime_resume(struct device *dev)
  1048. {
  1049. struct spi_master *master = dev_get_drvdata(dev);
  1050. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1051. int ret;
  1052. ret = clk_prepare_enable(tspi->clk);
  1053. if (ret < 0) {
  1054. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1055. return ret;
  1056. }
  1057. return 0;
  1058. }
  1059. static const struct dev_pm_ops slink_pm_ops = {
  1060. SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
  1061. tegra_slink_runtime_resume, NULL)
  1062. SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
  1063. };
  1064. static struct platform_driver tegra_slink_driver = {
  1065. .driver = {
  1066. .name = "spi-tegra-slink",
  1067. .pm = &slink_pm_ops,
  1068. .of_match_table = tegra_slink_of_match,
  1069. },
  1070. .probe = tegra_slink_probe,
  1071. .remove = tegra_slink_remove,
  1072. };
  1073. module_platform_driver(tegra_slink_driver);
  1074. MODULE_ALIAS("platform:spi-tegra-slink");
  1075. MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
  1076. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1077. MODULE_LICENSE("GPL v2");