spi-tegra20-slink.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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. pm_runtime_put_noidle(tspi->dev);
  666. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  667. return ret;
  668. }
  669. spin_lock_irqsave(&tspi->lock, flags);
  670. val = tspi->def_command_reg;
  671. if (spi->mode & SPI_CS_HIGH)
  672. val |= cs_pol_bit[spi->chip_select];
  673. else
  674. val &= ~cs_pol_bit[spi->chip_select];
  675. tspi->def_command_reg = val;
  676. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  677. spin_unlock_irqrestore(&tspi->lock, flags);
  678. pm_runtime_put(tspi->dev);
  679. return 0;
  680. }
  681. static int tegra_slink_prepare_message(struct spi_master *master,
  682. struct spi_message *msg)
  683. {
  684. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  685. struct spi_device *spi = msg->spi;
  686. tegra_slink_clear_status(tspi);
  687. tspi->command_reg = tspi->def_command_reg;
  688. tspi->command_reg |= SLINK_CS_SW | SLINK_CS_VALUE;
  689. tspi->command2_reg = tspi->def_command2_reg;
  690. tspi->command2_reg |= SLINK_SS_EN_CS(spi->chip_select);
  691. tspi->command_reg &= ~SLINK_MODES;
  692. if (spi->mode & SPI_CPHA)
  693. tspi->command_reg |= SLINK_CK_SDA;
  694. if (spi->mode & SPI_CPOL)
  695. tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_HIGH;
  696. else
  697. tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_LOW;
  698. return 0;
  699. }
  700. static int tegra_slink_transfer_one(struct spi_master *master,
  701. struct spi_device *spi,
  702. struct spi_transfer *xfer)
  703. {
  704. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  705. int ret;
  706. reinit_completion(&tspi->xfer_completion);
  707. ret = tegra_slink_start_transfer_one(spi, xfer);
  708. if (ret < 0) {
  709. dev_err(tspi->dev,
  710. "spi can not start transfer, err %d\n", ret);
  711. return ret;
  712. }
  713. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  714. SLINK_DMA_TIMEOUT);
  715. if (WARN_ON(ret == 0)) {
  716. dev_err(tspi->dev,
  717. "spi transfer timeout, err %d\n", ret);
  718. return -EIO;
  719. }
  720. if (tspi->tx_status)
  721. return tspi->tx_status;
  722. if (tspi->rx_status)
  723. return tspi->rx_status;
  724. return 0;
  725. }
  726. static int tegra_slink_unprepare_message(struct spi_master *master,
  727. struct spi_message *msg)
  728. {
  729. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  730. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  731. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  732. return 0;
  733. }
  734. static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
  735. {
  736. struct spi_transfer *t = tspi->curr_xfer;
  737. unsigned long flags;
  738. spin_lock_irqsave(&tspi->lock, flags);
  739. if (tspi->tx_status || tspi->rx_status ||
  740. (tspi->status_reg & SLINK_BSY)) {
  741. dev_err(tspi->dev,
  742. "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
  743. dev_err(tspi->dev,
  744. "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  745. tspi->command2_reg, tspi->dma_control_reg);
  746. reset_control_assert(tspi->rst);
  747. udelay(2);
  748. reset_control_deassert(tspi->rst);
  749. complete(&tspi->xfer_completion);
  750. goto exit;
  751. }
  752. if (tspi->cur_direction & DATA_DIR_RX)
  753. tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
  754. if (tspi->cur_direction & DATA_DIR_TX)
  755. tspi->cur_pos = tspi->cur_tx_pos;
  756. else
  757. tspi->cur_pos = tspi->cur_rx_pos;
  758. if (tspi->cur_pos == t->len) {
  759. complete(&tspi->xfer_completion);
  760. goto exit;
  761. }
  762. tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  763. tegra_slink_start_cpu_based_transfer(tspi, t);
  764. exit:
  765. spin_unlock_irqrestore(&tspi->lock, flags);
  766. return IRQ_HANDLED;
  767. }
  768. static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
  769. {
  770. struct spi_transfer *t = tspi->curr_xfer;
  771. long wait_status;
  772. int err = 0;
  773. unsigned total_fifo_words;
  774. unsigned long flags;
  775. /* Abort dmas if any error */
  776. if (tspi->cur_direction & DATA_DIR_TX) {
  777. if (tspi->tx_status) {
  778. dmaengine_terminate_all(tspi->tx_dma_chan);
  779. err += 1;
  780. } else {
  781. wait_status = wait_for_completion_interruptible_timeout(
  782. &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
  783. if (wait_status <= 0) {
  784. dmaengine_terminate_all(tspi->tx_dma_chan);
  785. dev_err(tspi->dev, "TxDma Xfer failed\n");
  786. err += 1;
  787. }
  788. }
  789. }
  790. if (tspi->cur_direction & DATA_DIR_RX) {
  791. if (tspi->rx_status) {
  792. dmaengine_terminate_all(tspi->rx_dma_chan);
  793. err += 2;
  794. } else {
  795. wait_status = wait_for_completion_interruptible_timeout(
  796. &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
  797. if (wait_status <= 0) {
  798. dmaengine_terminate_all(tspi->rx_dma_chan);
  799. dev_err(tspi->dev, "RxDma Xfer failed\n");
  800. err += 2;
  801. }
  802. }
  803. }
  804. spin_lock_irqsave(&tspi->lock, flags);
  805. if (err) {
  806. dev_err(tspi->dev,
  807. "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
  808. dev_err(tspi->dev,
  809. "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  810. tspi->command2_reg, tspi->dma_control_reg);
  811. reset_control_assert(tspi->rst);
  812. udelay(2);
  813. reset_control_assert(tspi->rst);
  814. complete(&tspi->xfer_completion);
  815. spin_unlock_irqrestore(&tspi->lock, flags);
  816. return IRQ_HANDLED;
  817. }
  818. if (tspi->cur_direction & DATA_DIR_RX)
  819. tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  820. if (tspi->cur_direction & DATA_DIR_TX)
  821. tspi->cur_pos = tspi->cur_tx_pos;
  822. else
  823. tspi->cur_pos = tspi->cur_rx_pos;
  824. if (tspi->cur_pos == t->len) {
  825. complete(&tspi->xfer_completion);
  826. goto exit;
  827. }
  828. /* Continue transfer in current message */
  829. total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
  830. tspi, t);
  831. if (total_fifo_words > SLINK_FIFO_DEPTH)
  832. err = tegra_slink_start_dma_based_transfer(tspi, t);
  833. else
  834. err = tegra_slink_start_cpu_based_transfer(tspi, t);
  835. exit:
  836. spin_unlock_irqrestore(&tspi->lock, flags);
  837. return IRQ_HANDLED;
  838. }
  839. static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
  840. {
  841. struct tegra_slink_data *tspi = context_data;
  842. if (!tspi->is_curr_dma_xfer)
  843. return handle_cpu_based_xfer(tspi);
  844. return handle_dma_based_xfer(tspi);
  845. }
  846. static irqreturn_t tegra_slink_isr(int irq, void *context_data)
  847. {
  848. struct tegra_slink_data *tspi = context_data;
  849. tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
  850. if (tspi->cur_direction & DATA_DIR_TX)
  851. tspi->tx_status = tspi->status_reg &
  852. (SLINK_TX_OVF | SLINK_TX_UNF);
  853. if (tspi->cur_direction & DATA_DIR_RX)
  854. tspi->rx_status = tspi->status_reg &
  855. (SLINK_RX_OVF | SLINK_RX_UNF);
  856. tegra_slink_clear_status(tspi);
  857. return IRQ_WAKE_THREAD;
  858. }
  859. static const struct tegra_slink_chip_data tegra30_spi_cdata = {
  860. .cs_hold_time = true,
  861. };
  862. static const struct tegra_slink_chip_data tegra20_spi_cdata = {
  863. .cs_hold_time = false,
  864. };
  865. static const struct of_device_id tegra_slink_of_match[] = {
  866. { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
  867. { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
  868. {}
  869. };
  870. MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
  871. static int tegra_slink_probe(struct platform_device *pdev)
  872. {
  873. struct spi_master *master;
  874. struct tegra_slink_data *tspi;
  875. struct resource *r;
  876. int ret, spi_irq;
  877. const struct tegra_slink_chip_data *cdata = NULL;
  878. const struct of_device_id *match;
  879. match = of_match_device(tegra_slink_of_match, &pdev->dev);
  880. if (!match) {
  881. dev_err(&pdev->dev, "Error: No device match found\n");
  882. return -ENODEV;
  883. }
  884. cdata = match->data;
  885. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  886. if (!master) {
  887. dev_err(&pdev->dev, "master allocation failed\n");
  888. return -ENOMEM;
  889. }
  890. /* the spi->mode bits understood by this driver: */
  891. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  892. master->setup = tegra_slink_setup;
  893. master->prepare_message = tegra_slink_prepare_message;
  894. master->transfer_one = tegra_slink_transfer_one;
  895. master->unprepare_message = tegra_slink_unprepare_message;
  896. master->auto_runtime_pm = true;
  897. master->num_chipselect = MAX_CHIP_SELECT;
  898. platform_set_drvdata(pdev, master);
  899. tspi = spi_master_get_devdata(master);
  900. tspi->master = master;
  901. tspi->dev = &pdev->dev;
  902. tspi->chip_data = cdata;
  903. spin_lock_init(&tspi->lock);
  904. if (of_property_read_u32(tspi->dev->of_node, "spi-max-frequency",
  905. &master->max_speed_hz))
  906. master->max_speed_hz = 25000000; /* 25MHz */
  907. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  908. if (!r) {
  909. dev_err(&pdev->dev, "No IO memory resource\n");
  910. ret = -ENODEV;
  911. goto exit_free_master;
  912. }
  913. tspi->phys = r->start;
  914. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  915. if (IS_ERR(tspi->base)) {
  916. ret = PTR_ERR(tspi->base);
  917. goto exit_free_master;
  918. }
  919. /* disabled clock may cause interrupt storm upon request */
  920. tspi->clk = devm_clk_get(&pdev->dev, NULL);
  921. if (IS_ERR(tspi->clk)) {
  922. ret = PTR_ERR(tspi->clk);
  923. dev_err(&pdev->dev, "Can not get clock %d\n", ret);
  924. goto exit_free_master;
  925. }
  926. ret = clk_prepare(tspi->clk);
  927. if (ret < 0) {
  928. dev_err(&pdev->dev, "Clock prepare failed %d\n", ret);
  929. goto exit_free_master;
  930. }
  931. ret = clk_enable(tspi->clk);
  932. if (ret < 0) {
  933. dev_err(&pdev->dev, "Clock enable failed %d\n", ret);
  934. goto exit_clk_unprepare;
  935. }
  936. spi_irq = platform_get_irq(pdev, 0);
  937. tspi->irq = spi_irq;
  938. ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
  939. tegra_slink_isr_thread, IRQF_ONESHOT,
  940. dev_name(&pdev->dev), tspi);
  941. if (ret < 0) {
  942. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  943. tspi->irq);
  944. goto exit_clk_disable;
  945. }
  946. tspi->rst = devm_reset_control_get_exclusive(&pdev->dev, "spi");
  947. if (IS_ERR(tspi->rst)) {
  948. dev_err(&pdev->dev, "can not get reset\n");
  949. ret = PTR_ERR(tspi->rst);
  950. goto exit_free_irq;
  951. }
  952. tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
  953. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  954. ret = tegra_slink_init_dma_param(tspi, true);
  955. if (ret < 0)
  956. goto exit_free_irq;
  957. ret = tegra_slink_init_dma_param(tspi, false);
  958. if (ret < 0)
  959. goto exit_rx_dma_free;
  960. tspi->max_buf_size = tspi->dma_buf_size;
  961. init_completion(&tspi->tx_dma_complete);
  962. init_completion(&tspi->rx_dma_complete);
  963. init_completion(&tspi->xfer_completion);
  964. pm_runtime_enable(&pdev->dev);
  965. if (!pm_runtime_enabled(&pdev->dev)) {
  966. ret = tegra_slink_runtime_resume(&pdev->dev);
  967. if (ret)
  968. goto exit_pm_disable;
  969. }
  970. ret = pm_runtime_get_sync(&pdev->dev);
  971. if (ret < 0) {
  972. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  973. goto exit_pm_disable;
  974. }
  975. tspi->def_command_reg = SLINK_M_S;
  976. tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
  977. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  978. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  979. pm_runtime_put(&pdev->dev);
  980. master->dev.of_node = pdev->dev.of_node;
  981. ret = devm_spi_register_master(&pdev->dev, master);
  982. if (ret < 0) {
  983. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  984. goto exit_pm_disable;
  985. }
  986. return ret;
  987. exit_pm_disable:
  988. pm_runtime_disable(&pdev->dev);
  989. if (!pm_runtime_status_suspended(&pdev->dev))
  990. tegra_slink_runtime_suspend(&pdev->dev);
  991. tegra_slink_deinit_dma_param(tspi, false);
  992. exit_rx_dma_free:
  993. tegra_slink_deinit_dma_param(tspi, true);
  994. exit_free_irq:
  995. free_irq(spi_irq, tspi);
  996. exit_clk_disable:
  997. clk_disable(tspi->clk);
  998. exit_clk_unprepare:
  999. clk_unprepare(tspi->clk);
  1000. exit_free_master:
  1001. spi_master_put(master);
  1002. return ret;
  1003. }
  1004. static int tegra_slink_remove(struct platform_device *pdev)
  1005. {
  1006. struct spi_master *master = platform_get_drvdata(pdev);
  1007. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1008. free_irq(tspi->irq, tspi);
  1009. clk_disable(tspi->clk);
  1010. clk_unprepare(tspi->clk);
  1011. if (tspi->tx_dma_chan)
  1012. tegra_slink_deinit_dma_param(tspi, false);
  1013. if (tspi->rx_dma_chan)
  1014. tegra_slink_deinit_dma_param(tspi, true);
  1015. pm_runtime_disable(&pdev->dev);
  1016. if (!pm_runtime_status_suspended(&pdev->dev))
  1017. tegra_slink_runtime_suspend(&pdev->dev);
  1018. return 0;
  1019. }
  1020. #ifdef CONFIG_PM_SLEEP
  1021. static int tegra_slink_suspend(struct device *dev)
  1022. {
  1023. struct spi_master *master = dev_get_drvdata(dev);
  1024. return spi_master_suspend(master);
  1025. }
  1026. static int tegra_slink_resume(struct device *dev)
  1027. {
  1028. struct spi_master *master = dev_get_drvdata(dev);
  1029. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1030. int ret;
  1031. ret = pm_runtime_get_sync(dev);
  1032. if (ret < 0) {
  1033. pm_runtime_put_noidle(dev);
  1034. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1035. return ret;
  1036. }
  1037. tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
  1038. tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
  1039. pm_runtime_put(dev);
  1040. return spi_master_resume(master);
  1041. }
  1042. #endif
  1043. static int tegra_slink_runtime_suspend(struct device *dev)
  1044. {
  1045. struct spi_master *master = dev_get_drvdata(dev);
  1046. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1047. /* Flush all write which are in PPSB queue by reading back */
  1048. tegra_slink_readl(tspi, SLINK_MAS_DATA);
  1049. clk_disable_unprepare(tspi->clk);
  1050. return 0;
  1051. }
  1052. static int tegra_slink_runtime_resume(struct device *dev)
  1053. {
  1054. struct spi_master *master = dev_get_drvdata(dev);
  1055. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1056. int ret;
  1057. ret = clk_prepare_enable(tspi->clk);
  1058. if (ret < 0) {
  1059. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1060. return ret;
  1061. }
  1062. return 0;
  1063. }
  1064. static const struct dev_pm_ops slink_pm_ops = {
  1065. SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
  1066. tegra_slink_runtime_resume, NULL)
  1067. SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
  1068. };
  1069. static struct platform_driver tegra_slink_driver = {
  1070. .driver = {
  1071. .name = "spi-tegra-slink",
  1072. .pm = &slink_pm_ops,
  1073. .of_match_table = tegra_slink_of_match,
  1074. },
  1075. .probe = tegra_slink_probe,
  1076. .remove = tegra_slink_remove,
  1077. };
  1078. module_platform_driver(tegra_slink_driver);
  1079. MODULE_ALIAS("platform:spi-tegra-slink");
  1080. MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
  1081. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1082. MODULE_LICENSE("GPL v2");