qcom-geni-se.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
  4. * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
  5. */
  6. /* Disable MMIO tracing to prevent excessive logging of unwanted MMIO traces */
  7. #define __DISABLE_TRACE_MMIO__
  8. #include <linux/acpi.h>
  9. #include <linux/bitfield.h>
  10. #include <linux/clk.h>
  11. #include <linux/firmware.h>
  12. #include <linux/slab.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/soc/qcom/geni-se.h>
  21. /**
  22. * DOC: Overview
  23. *
  24. * Generic Interface (GENI) Serial Engine (SE) Wrapper driver is introduced
  25. * to manage GENI firmware based Qualcomm Universal Peripheral (QUP) Wrapper
  26. * controller. QUP Wrapper is designed to support various serial bus protocols
  27. * like UART, SPI, I2C, I3C, etc.
  28. */
  29. /**
  30. * DOC: Hardware description
  31. *
  32. * GENI based QUP is a highly-flexible and programmable module for supporting
  33. * a wide range of serial interfaces like UART, SPI, I2C, I3C, etc. A single
  34. * QUP module can provide upto 8 serial interfaces, using its internal
  35. * serial engines. The actual configuration is determined by the target
  36. * platform configuration. The protocol supported by each interface is
  37. * determined by the firmware loaded to the serial engine. Each SE consists
  38. * of a DMA Engine and GENI sub modules which enable serial engines to
  39. * support FIFO and DMA modes of operation.
  40. *
  41. *
  42. * +-----------------------------------------+
  43. * |QUP Wrapper |
  44. * | +----------------------------+ |
  45. * --QUP & SE Clocks--> | Serial Engine N | +-IO------>
  46. * | | ... | | Interface
  47. * <---Clock Perf.----+ +----+-----------------------+ | |
  48. * State Interface | | Serial Engine 1 | | |
  49. * | | | | |
  50. * | | | | |
  51. * <--------AHB-------> | | | |
  52. * | | +----+ |
  53. * | | | |
  54. * | | | |
  55. * <------SE IRQ------+ +----------------------------+ |
  56. * | |
  57. * +-----------------------------------------+
  58. *
  59. * Figure 1: GENI based QUP Wrapper
  60. *
  61. * The GENI submodules include primary and secondary sequencers which are
  62. * used to drive TX & RX operations. On serial interfaces that operate using
  63. * master-slave model, primary sequencer drives both TX & RX operations. On
  64. * serial interfaces that operate using peer-to-peer model, primary sequencer
  65. * drives TX operation and secondary sequencer drives RX operation.
  66. */
  67. /**
  68. * DOC: Software description
  69. *
  70. * GENI SE Wrapper driver is structured into 2 parts:
  71. *
  72. * geni_wrapper represents QUP Wrapper controller. This part of the driver
  73. * manages QUP Wrapper information such as hardware version, clock
  74. * performance table that is common to all the internal serial engines.
  75. *
  76. * geni_se represents serial engine. This part of the driver manages serial
  77. * engine information such as clocks, containing QUP Wrapper, etc. This part
  78. * of driver also supports operations (eg. initialize the concerned serial
  79. * engine, select between FIFO and DMA mode of operation etc.) that are
  80. * common to all the serial engines and are independent of serial interfaces.
  81. */
  82. #define MAX_CLK_PERF_LEVEL 32
  83. #define MAX_CLKS 2
  84. /**
  85. * struct geni_wrapper - Data structure to represent the QUP Wrapper Core
  86. * @dev: Device pointer of the QUP wrapper core
  87. * @base: Base address of this instance of QUP wrapper core
  88. * @clks: Handle to the primary & optional secondary AHB clocks
  89. * @num_clks: Count of clocks
  90. */
  91. struct geni_wrapper {
  92. struct device *dev;
  93. void __iomem *base;
  94. struct clk_bulk_data clks[MAX_CLKS];
  95. unsigned int num_clks;
  96. };
  97. /**
  98. * struct geni_se_desc - Data structure to represent the QUP Wrapper resources
  99. * @clks: Name of the primary & optional secondary AHB clocks
  100. * @num_clks: Count of clock names
  101. */
  102. struct geni_se_desc {
  103. unsigned int num_clks;
  104. const char * const *clks;
  105. };
  106. static const char * const icc_path_names[] = {"qup-core", "qup-config",
  107. "qup-memory"};
  108. static const char * const protocol_name[] = { "None", "SPI", "UART", "I2C", "I3C", "SPI SLAVE" };
  109. /**
  110. * struct se_fw_hdr - Serial Engine firmware configuration header
  111. *
  112. * This structure defines the SE firmware header, which together with the
  113. * firmware payload is stored in individual ELF segments.
  114. *
  115. * @magic: Set to 'SEFW'.
  116. * @version: Structure version number.
  117. * @core_version: QUPV3 hardware version.
  118. * @serial_protocol: Encoded in GENI_FW_REVISION.
  119. * @fw_version: Firmware version, from GENI_FW_REVISION.
  120. * @cfg_version: Configuration version, from GENI_INIT_CFG_REVISION.
  121. * @fw_size_in_items: Number of 32-bit words in GENI_FW_RAM.
  122. * @fw_offset: Byte offset to GENI_FW_RAM array.
  123. * @cfg_size_in_items: Number of GENI_FW_CFG index/value pairs.
  124. * @cfg_idx_offset: Byte offset to GENI_FW_CFG index array.
  125. * @cfg_val_offset: Byte offset to GENI_FW_CFG values array.
  126. */
  127. struct se_fw_hdr {
  128. __le32 magic;
  129. __le32 version;
  130. __le32 core_version;
  131. __le16 serial_protocol;
  132. __le16 fw_version;
  133. __le16 cfg_version;
  134. __le16 fw_size_in_items;
  135. __le16 fw_offset;
  136. __le16 cfg_size_in_items;
  137. __le16 cfg_idx_offset;
  138. __le16 cfg_val_offset;
  139. };
  140. /*Magic numbers*/
  141. #define SE_MAGIC_NUM 0x57464553
  142. #define MAX_GENI_CFG_RAMn_CNT 455
  143. #define MI_PBT_NON_PAGED_SEGMENT 0x0
  144. #define MI_PBT_HASH_SEGMENT 0x2
  145. #define MI_PBT_NOTUSED_SEGMENT 0x3
  146. #define MI_PBT_SHARED_SEGMENT 0x4
  147. #define MI_PBT_FLAG_PAGE_MODE BIT(20)
  148. #define MI_PBT_FLAG_SEGMENT_TYPE GENMASK(26, 24)
  149. #define MI_PBT_FLAG_ACCESS_TYPE GENMASK(23, 21)
  150. #define MI_PBT_PAGE_MODE_VALUE(x) FIELD_GET(MI_PBT_FLAG_PAGE_MODE, x)
  151. #define MI_PBT_SEGMENT_TYPE_VALUE(x) FIELD_GET(MI_PBT_FLAG_SEGMENT_TYPE, x)
  152. #define MI_PBT_ACCESS_TYPE_VALUE(x) FIELD_GET(MI_PBT_FLAG_ACCESS_TYPE, x)
  153. #define M_COMMON_GENI_M_IRQ_EN (GENMASK(6, 1) | \
  154. M_IO_DATA_DEASSERT_EN | \
  155. M_IO_DATA_ASSERT_EN | M_RX_FIFO_RD_ERR_EN | \
  156. M_RX_FIFO_WR_ERR_EN | M_TX_FIFO_RD_ERR_EN | \
  157. M_TX_FIFO_WR_ERR_EN)
  158. /* Common QUPV3 registers */
  159. #define QUPV3_HW_VER_REG 0x4
  160. #define QUPV3_SE_AHB_M_CFG 0x118
  161. #define QUPV3_COMMON_CFG 0x120
  162. #define QUPV3_COMMON_CGC_CTRL 0x21c
  163. /* QUPV3_COMMON_CFG fields */
  164. #define FAST_SWITCH_TO_HIGH_DISABLE BIT(0)
  165. /* QUPV3_SE_AHB_M_CFG fields */
  166. #define AHB_M_CLK_CGC_ON BIT(0)
  167. /* QUPV3_COMMON_CGC_CTRL fields */
  168. #define COMMON_CSR_SLV_CLK_CGC_ON BIT(0)
  169. /* Common SE registers */
  170. #define SE_GENI_INIT_CFG_REVISION 0x0
  171. #define SE_GENI_S_INIT_CFG_REVISION 0x4
  172. #define SE_GENI_CGC_CTRL 0x28
  173. #define SE_GENI_CLK_CTRL_RO 0x60
  174. #define SE_GENI_FW_S_REVISION_RO 0x6c
  175. #define SE_GENI_CFG_REG0 0x100
  176. #define SE_GENI_BYTE_GRAN 0x254
  177. #define SE_GENI_TX_PACKING_CFG0 0x260
  178. #define SE_GENI_TX_PACKING_CFG1 0x264
  179. #define SE_GENI_RX_PACKING_CFG0 0x284
  180. #define SE_GENI_RX_PACKING_CFG1 0x288
  181. #define SE_GENI_S_IRQ_ENABLE 0x644
  182. #define SE_DMA_TX_PTR_L 0xc30
  183. #define SE_DMA_TX_PTR_H 0xc34
  184. #define SE_DMA_TX_ATTR 0xc38
  185. #define SE_DMA_TX_LEN 0xc3c
  186. #define SE_DMA_TX_IRQ_EN 0xc48
  187. #define SE_DMA_TX_IRQ_EN_SET 0xc4c
  188. #define SE_DMA_TX_IRQ_EN_CLR 0xc50
  189. #define SE_DMA_TX_LEN_IN 0xc54
  190. #define SE_DMA_TX_MAX_BURST 0xc5c
  191. #define SE_DMA_RX_PTR_L 0xd30
  192. #define SE_DMA_RX_PTR_H 0xd34
  193. #define SE_DMA_RX_ATTR 0xd38
  194. #define SE_DMA_RX_LEN 0xd3c
  195. #define SE_DMA_RX_IRQ_EN 0xd48
  196. #define SE_DMA_RX_IRQ_EN_SET 0xd4c
  197. #define SE_DMA_RX_IRQ_EN_CLR 0xd50
  198. #define SE_DMA_RX_MAX_BURST 0xd5c
  199. #define SE_DMA_RX_FLUSH 0xd60
  200. #define SE_GSI_EVENT_EN 0xe18
  201. #define SE_IRQ_EN 0xe1c
  202. #define SE_DMA_GENERAL_CFG 0xe30
  203. #define SE_GENI_FW_REVISION 0x1000
  204. #define SE_GENI_S_FW_REVISION 0x1004
  205. #define SE_GENI_CFG_RAMN 0x1010
  206. #define SE_GENI_CLK_CTRL 0x2000
  207. #define SE_DMA_IF_EN 0x2004
  208. #define SE_FIFO_IF_DISABLE 0x2008
  209. /* GENI_FW_REVISION_RO fields */
  210. #define FW_REV_VERSION_MSK GENMASK(7, 0)
  211. /* GENI_OUTPUT_CTRL fields */
  212. #define DEFAULT_IO_OUTPUT_CTRL_MSK GENMASK(6, 0)
  213. /* GENI_CGC_CTRL fields */
  214. #define CFG_AHB_CLK_CGC_ON BIT(0)
  215. #define CFG_AHB_WR_ACLK_CGC_ON BIT(1)
  216. #define DATA_AHB_CLK_CGC_ON BIT(2)
  217. #define SCLK_CGC_ON BIT(3)
  218. #define TX_CLK_CGC_ON BIT(4)
  219. #define RX_CLK_CGC_ON BIT(5)
  220. #define EXT_CLK_CGC_ON BIT(6)
  221. #define PROG_RAM_HCLK_OFF BIT(8)
  222. #define PROG_RAM_SCLK_OFF BIT(9)
  223. #define DEFAULT_CGC_EN GENMASK(6, 0)
  224. /* SE_GSI_EVENT_EN fields */
  225. #define DMA_RX_EVENT_EN BIT(0)
  226. #define DMA_TX_EVENT_EN BIT(1)
  227. #define GENI_M_EVENT_EN BIT(2)
  228. #define GENI_S_EVENT_EN BIT(3)
  229. /* SE_IRQ_EN fields */
  230. #define DMA_RX_IRQ_EN BIT(0)
  231. #define DMA_TX_IRQ_EN BIT(1)
  232. #define GENI_M_IRQ_EN BIT(2)
  233. #define GENI_S_IRQ_EN BIT(3)
  234. /* SE_DMA_GENERAL_CFG */
  235. #define DMA_RX_CLK_CGC_ON BIT(0)
  236. #define DMA_TX_CLK_CGC_ON BIT(1)
  237. #define DMA_AHB_SLV_CLK_CGC_ON BIT(2)
  238. #define AHB_SEC_SLV_CLK_CGC_ON BIT(3)
  239. #define DUMMY_RX_NON_BUFFERABLE BIT(4)
  240. #define RX_DMA_ZERO_PADDING_EN BIT(5)
  241. #define RX_DMA_IRQ_DELAY_MSK GENMASK(8, 6)
  242. #define RX_DMA_IRQ_DELAY_SHFT 6
  243. /* GENI_CLK_CTRL fields */
  244. #define SER_CLK_SEL BIT(0)
  245. /* GENI_DMA_IF_EN fields */
  246. #define DMA_IF_EN BIT(0)
  247. #define geni_setbits32(_addr, _v) writel(readl(_addr) | (_v), _addr)
  248. #define geni_clrbits32(_addr, _v) writel(readl(_addr) & ~(_v), _addr)
  249. /**
  250. * geni_se_get_qup_hw_version() - Read the QUP wrapper Hardware version
  251. * @se: Pointer to the corresponding serial engine.
  252. *
  253. * Return: Hardware Version of the wrapper.
  254. */
  255. u32 geni_se_get_qup_hw_version(struct geni_se *se)
  256. {
  257. struct geni_wrapper *wrapper = se->wrapper;
  258. return readl_relaxed(wrapper->base + QUPV3_HW_VER_REG);
  259. }
  260. EXPORT_SYMBOL_GPL(geni_se_get_qup_hw_version);
  261. static void geni_se_io_set_mode(void __iomem *base)
  262. {
  263. u32 val;
  264. val = readl_relaxed(base + SE_IRQ_EN);
  265. val |= GENI_M_IRQ_EN | GENI_S_IRQ_EN;
  266. val |= DMA_TX_IRQ_EN | DMA_RX_IRQ_EN;
  267. writel_relaxed(val, base + SE_IRQ_EN);
  268. val = readl_relaxed(base + SE_GENI_DMA_MODE_EN);
  269. val &= ~GENI_DMA_MODE_EN;
  270. writel_relaxed(val, base + SE_GENI_DMA_MODE_EN);
  271. writel_relaxed(0, base + SE_GSI_EVENT_EN);
  272. }
  273. static void geni_se_io_init(void __iomem *base)
  274. {
  275. u32 val;
  276. val = readl_relaxed(base + SE_GENI_CGC_CTRL);
  277. val |= DEFAULT_CGC_EN;
  278. writel_relaxed(val, base + SE_GENI_CGC_CTRL);
  279. val = readl_relaxed(base + SE_DMA_GENERAL_CFG);
  280. val |= AHB_SEC_SLV_CLK_CGC_ON | DMA_AHB_SLV_CLK_CGC_ON;
  281. val |= DMA_TX_CLK_CGC_ON | DMA_RX_CLK_CGC_ON;
  282. writel_relaxed(val, base + SE_DMA_GENERAL_CFG);
  283. writel_relaxed(DEFAULT_IO_OUTPUT_CTRL_MSK, base + GENI_OUTPUT_CTRL);
  284. writel_relaxed(FORCE_DEFAULT, base + GENI_FORCE_DEFAULT_REG);
  285. }
  286. static void geni_se_irq_clear(struct geni_se *se)
  287. {
  288. writel_relaxed(0, se->base + SE_GSI_EVENT_EN);
  289. writel_relaxed(0xffffffff, se->base + SE_GENI_M_IRQ_CLEAR);
  290. writel_relaxed(0xffffffff, se->base + SE_GENI_S_IRQ_CLEAR);
  291. writel_relaxed(0xffffffff, se->base + SE_DMA_TX_IRQ_CLR);
  292. writel_relaxed(0xffffffff, se->base + SE_DMA_RX_IRQ_CLR);
  293. writel_relaxed(0xffffffff, se->base + SE_IRQ_EN);
  294. }
  295. /**
  296. * geni_se_init() - Initialize the GENI serial engine
  297. * @se: Pointer to the concerned serial engine.
  298. * @rx_wm: Receive watermark, in units of FIFO words.
  299. * @rx_rfr: Ready-for-receive watermark, in units of FIFO words.
  300. *
  301. * This function is used to initialize the GENI serial engine, configure
  302. * receive watermark and ready-for-receive watermarks.
  303. */
  304. void geni_se_init(struct geni_se *se, u32 rx_wm, u32 rx_rfr)
  305. {
  306. u32 val;
  307. geni_se_irq_clear(se);
  308. geni_se_io_init(se->base);
  309. geni_se_io_set_mode(se->base);
  310. writel_relaxed(rx_wm, se->base + SE_GENI_RX_WATERMARK_REG);
  311. writel_relaxed(rx_rfr, se->base + SE_GENI_RX_RFR_WATERMARK_REG);
  312. val = readl_relaxed(se->base + SE_GENI_M_IRQ_EN);
  313. val |= M_COMMON_GENI_M_IRQ_EN;
  314. writel_relaxed(val, se->base + SE_GENI_M_IRQ_EN);
  315. val = readl_relaxed(se->base + SE_GENI_S_IRQ_EN);
  316. val |= S_COMMON_GENI_S_IRQ_EN;
  317. writel_relaxed(val, se->base + SE_GENI_S_IRQ_EN);
  318. }
  319. EXPORT_SYMBOL_GPL(geni_se_init);
  320. static void geni_se_select_fifo_mode(struct geni_se *se)
  321. {
  322. u32 proto = geni_se_read_proto(se);
  323. u32 val, val_old;
  324. geni_se_irq_clear(se);
  325. /* UART driver manages enabling / disabling interrupts internally */
  326. if (proto != GENI_SE_UART) {
  327. /* Non-UART use only primary sequencer so dont bother about S_IRQ */
  328. val_old = val = readl_relaxed(se->base + SE_GENI_M_IRQ_EN);
  329. val |= M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN;
  330. val |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
  331. if (val != val_old)
  332. writel_relaxed(val, se->base + SE_GENI_M_IRQ_EN);
  333. }
  334. val_old = val = readl_relaxed(se->base + SE_GENI_DMA_MODE_EN);
  335. val &= ~GENI_DMA_MODE_EN;
  336. if (val != val_old)
  337. writel_relaxed(val, se->base + SE_GENI_DMA_MODE_EN);
  338. }
  339. static void geni_se_select_dma_mode(struct geni_se *se)
  340. {
  341. u32 proto = geni_se_read_proto(se);
  342. u32 val, val_old;
  343. geni_se_irq_clear(se);
  344. /* UART driver manages enabling / disabling interrupts internally */
  345. if (proto != GENI_SE_UART) {
  346. /* Non-UART use only primary sequencer so dont bother about S_IRQ */
  347. val_old = val = readl_relaxed(se->base + SE_GENI_M_IRQ_EN);
  348. val &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
  349. val &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
  350. if (val != val_old)
  351. writel_relaxed(val, se->base + SE_GENI_M_IRQ_EN);
  352. }
  353. val_old = val = readl_relaxed(se->base + SE_GENI_DMA_MODE_EN);
  354. val |= GENI_DMA_MODE_EN;
  355. if (val != val_old)
  356. writel_relaxed(val, se->base + SE_GENI_DMA_MODE_EN);
  357. }
  358. static void geni_se_select_gpi_mode(struct geni_se *se)
  359. {
  360. u32 val;
  361. geni_se_irq_clear(se);
  362. writel(0, se->base + SE_IRQ_EN);
  363. val = readl(se->base + SE_GENI_M_IRQ_EN);
  364. val &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN |
  365. M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
  366. writel(val, se->base + SE_GENI_M_IRQ_EN);
  367. writel(GENI_DMA_MODE_EN, se->base + SE_GENI_DMA_MODE_EN);
  368. val = readl(se->base + SE_GSI_EVENT_EN);
  369. val |= (DMA_RX_EVENT_EN | DMA_TX_EVENT_EN | GENI_M_EVENT_EN | GENI_S_EVENT_EN);
  370. writel(val, se->base + SE_GSI_EVENT_EN);
  371. }
  372. /**
  373. * geni_se_select_mode() - Select the serial engine transfer mode
  374. * @se: Pointer to the concerned serial engine.
  375. * @mode: Transfer mode to be selected.
  376. */
  377. void geni_se_select_mode(struct geni_se *se, enum geni_se_xfer_mode mode)
  378. {
  379. WARN_ON(mode != GENI_SE_FIFO && mode != GENI_SE_DMA && mode != GENI_GPI_DMA);
  380. switch (mode) {
  381. case GENI_SE_FIFO:
  382. geni_se_select_fifo_mode(se);
  383. break;
  384. case GENI_SE_DMA:
  385. geni_se_select_dma_mode(se);
  386. break;
  387. case GENI_GPI_DMA:
  388. geni_se_select_gpi_mode(se);
  389. break;
  390. case GENI_SE_INVALID:
  391. default:
  392. break;
  393. }
  394. }
  395. EXPORT_SYMBOL_GPL(geni_se_select_mode);
  396. /**
  397. * DOC: Overview
  398. *
  399. * GENI FIFO packing is highly configurable. TX/RX packing/unpacking consist
  400. * of up to 4 operations, each operation represented by 4 configuration vectors
  401. * of 10 bits programmed in GENI_TX_PACKING_CFG0 and GENI_TX_PACKING_CFG1 for
  402. * TX FIFO and in GENI_RX_PACKING_CFG0 and GENI_RX_PACKING_CFG1 for RX FIFO.
  403. * Refer to below examples for detailed bit-field description.
  404. *
  405. * Example 1: word_size = 7, packing_mode = 4 x 8, msb_to_lsb = 1
  406. *
  407. * +-----------+-------+-------+-------+-------+
  408. * | | vec_0 | vec_1 | vec_2 | vec_3 |
  409. * +-----------+-------+-------+-------+-------+
  410. * | start | 0x6 | 0xe | 0x16 | 0x1e |
  411. * | direction | 1 | 1 | 1 | 1 |
  412. * | length | 6 | 6 | 6 | 6 |
  413. * | stop | 0 | 0 | 0 | 1 |
  414. * +-----------+-------+-------+-------+-------+
  415. *
  416. * Example 2: word_size = 15, packing_mode = 2 x 16, msb_to_lsb = 0
  417. *
  418. * +-----------+-------+-------+-------+-------+
  419. * | | vec_0 | vec_1 | vec_2 | vec_3 |
  420. * +-----------+-------+-------+-------+-------+
  421. * | start | 0x0 | 0x8 | 0x10 | 0x18 |
  422. * | direction | 0 | 0 | 0 | 0 |
  423. * | length | 7 | 6 | 7 | 6 |
  424. * | stop | 0 | 0 | 0 | 1 |
  425. * +-----------+-------+-------+-------+-------+
  426. *
  427. * Example 3: word_size = 23, packing_mode = 1 x 32, msb_to_lsb = 1
  428. *
  429. * +-----------+-------+-------+-------+-------+
  430. * | | vec_0 | vec_1 | vec_2 | vec_3 |
  431. * +-----------+-------+-------+-------+-------+
  432. * | start | 0x16 | 0xe | 0x6 | 0x0 |
  433. * | direction | 1 | 1 | 1 | 1 |
  434. * | length | 7 | 7 | 6 | 0 |
  435. * | stop | 0 | 0 | 1 | 0 |
  436. * +-----------+-------+-------+-------+-------+
  437. *
  438. */
  439. #define NUM_PACKING_VECTORS 4
  440. #define PACKING_START_SHIFT 5
  441. #define PACKING_DIR_SHIFT 4
  442. #define PACKING_LEN_SHIFT 1
  443. #define PACKING_STOP_BIT BIT(0)
  444. #define PACKING_VECTOR_SHIFT 10
  445. /**
  446. * geni_se_config_packing() - Packing configuration of the serial engine
  447. * @se: Pointer to the concerned serial engine
  448. * @bpw: Bits of data per transfer word.
  449. * @pack_words: Number of words per fifo element.
  450. * @msb_to_lsb: Transfer from MSB to LSB or vice-versa.
  451. * @tx_cfg: Flag to configure the TX Packing.
  452. * @rx_cfg: Flag to configure the RX Packing.
  453. *
  454. * This function is used to configure the packing rules for the current
  455. * transfer.
  456. */
  457. void geni_se_config_packing(struct geni_se *se, int bpw, int pack_words,
  458. bool msb_to_lsb, bool tx_cfg, bool rx_cfg)
  459. {
  460. u32 cfg0, cfg1, cfg[NUM_PACKING_VECTORS] = {0};
  461. int len;
  462. int temp_bpw = bpw;
  463. int idx_start = msb_to_lsb ? bpw - 1 : 0;
  464. int idx = idx_start;
  465. int idx_delta = msb_to_lsb ? -BITS_PER_BYTE : BITS_PER_BYTE;
  466. int ceil_bpw = ALIGN(bpw, BITS_PER_BYTE);
  467. int iter = (ceil_bpw * pack_words) / BITS_PER_BYTE;
  468. int i;
  469. if (iter <= 0 || iter > NUM_PACKING_VECTORS)
  470. return;
  471. for (i = 0; i < iter; i++) {
  472. len = min_t(int, temp_bpw, BITS_PER_BYTE) - 1;
  473. cfg[i] = idx << PACKING_START_SHIFT;
  474. cfg[i] |= msb_to_lsb << PACKING_DIR_SHIFT;
  475. cfg[i] |= len << PACKING_LEN_SHIFT;
  476. if (temp_bpw <= BITS_PER_BYTE) {
  477. idx = ((i + 1) * BITS_PER_BYTE) + idx_start;
  478. temp_bpw = bpw;
  479. } else {
  480. idx = idx + idx_delta;
  481. temp_bpw = temp_bpw - BITS_PER_BYTE;
  482. }
  483. }
  484. cfg[iter - 1] |= PACKING_STOP_BIT;
  485. cfg0 = cfg[0] | (cfg[1] << PACKING_VECTOR_SHIFT);
  486. cfg1 = cfg[2] | (cfg[3] << PACKING_VECTOR_SHIFT);
  487. if (tx_cfg) {
  488. writel_relaxed(cfg0, se->base + SE_GENI_TX_PACKING_CFG0);
  489. writel_relaxed(cfg1, se->base + SE_GENI_TX_PACKING_CFG1);
  490. }
  491. if (rx_cfg) {
  492. writel_relaxed(cfg0, se->base + SE_GENI_RX_PACKING_CFG0);
  493. writel_relaxed(cfg1, se->base + SE_GENI_RX_PACKING_CFG1);
  494. }
  495. /*
  496. * Number of protocol words in each FIFO entry
  497. * 0 - 4x8, four words in each entry, max word size of 8 bits
  498. * 1 - 2x16, two words in each entry, max word size of 16 bits
  499. * 2 - 1x32, one word in each entry, max word size of 32 bits
  500. * 3 - undefined
  501. */
  502. if (pack_words || bpw == 32)
  503. writel_relaxed(bpw / 16, se->base + SE_GENI_BYTE_GRAN);
  504. }
  505. EXPORT_SYMBOL_GPL(geni_se_config_packing);
  506. static void geni_se_clks_off(struct geni_se *se)
  507. {
  508. struct geni_wrapper *wrapper = se->wrapper;
  509. clk_disable_unprepare(se->clk);
  510. clk_bulk_disable_unprepare(wrapper->num_clks, wrapper->clks);
  511. }
  512. /**
  513. * geni_se_resources_off() - Turn off resources associated with the serial
  514. * engine
  515. * @se: Pointer to the concerned serial engine.
  516. *
  517. * Return: 0 on success, standard Linux error codes on failure/error.
  518. */
  519. int geni_se_resources_off(struct geni_se *se)
  520. {
  521. int ret;
  522. if (has_acpi_companion(se->dev))
  523. return 0;
  524. ret = pinctrl_pm_select_sleep_state(se->dev);
  525. if (ret)
  526. return ret;
  527. geni_se_clks_off(se);
  528. return 0;
  529. }
  530. EXPORT_SYMBOL_GPL(geni_se_resources_off);
  531. static int geni_se_clks_on(struct geni_se *se)
  532. {
  533. int ret;
  534. struct geni_wrapper *wrapper = se->wrapper;
  535. ret = clk_bulk_prepare_enable(wrapper->num_clks, wrapper->clks);
  536. if (ret)
  537. return ret;
  538. ret = clk_prepare_enable(se->clk);
  539. if (ret)
  540. clk_bulk_disable_unprepare(wrapper->num_clks, wrapper->clks);
  541. return ret;
  542. }
  543. /**
  544. * geni_se_resources_on() - Turn on resources associated with the serial
  545. * engine
  546. * @se: Pointer to the concerned serial engine.
  547. *
  548. * Return: 0 on success, standard Linux error codes on failure/error.
  549. */
  550. int geni_se_resources_on(struct geni_se *se)
  551. {
  552. int ret;
  553. if (has_acpi_companion(se->dev))
  554. return 0;
  555. ret = geni_se_clks_on(se);
  556. if (ret)
  557. return ret;
  558. ret = pinctrl_pm_select_default_state(se->dev);
  559. if (ret)
  560. geni_se_clks_off(se);
  561. return ret;
  562. }
  563. EXPORT_SYMBOL_GPL(geni_se_resources_on);
  564. /**
  565. * geni_se_clk_tbl_get() - Get the clock table to program DFS
  566. * @se: Pointer to the concerned serial engine.
  567. * @tbl: Table in which the output is returned.
  568. *
  569. * This function is called by the protocol drivers to determine the different
  570. * clock frequencies supported by serial engine core clock. The protocol
  571. * drivers use the output to determine the clock frequency index to be
  572. * programmed into DFS.
  573. *
  574. * Return: number of valid performance levels in the table on success,
  575. * standard Linux error codes on failure.
  576. */
  577. int geni_se_clk_tbl_get(struct geni_se *se, unsigned long **tbl)
  578. {
  579. long freq = 0;
  580. int i;
  581. if (se->clk_perf_tbl) {
  582. *tbl = se->clk_perf_tbl;
  583. return se->num_clk_levels;
  584. }
  585. se->clk_perf_tbl = devm_kcalloc(se->dev, MAX_CLK_PERF_LEVEL,
  586. sizeof(*se->clk_perf_tbl),
  587. GFP_KERNEL);
  588. if (!se->clk_perf_tbl)
  589. return -ENOMEM;
  590. for (i = 0; i < MAX_CLK_PERF_LEVEL; i++) {
  591. freq = clk_round_rate(se->clk, freq + 1);
  592. if (freq <= 0 ||
  593. (i > 0 && freq == se->clk_perf_tbl[i - 1]))
  594. break;
  595. se->clk_perf_tbl[i] = freq;
  596. }
  597. se->num_clk_levels = i;
  598. *tbl = se->clk_perf_tbl;
  599. return se->num_clk_levels;
  600. }
  601. EXPORT_SYMBOL_GPL(geni_se_clk_tbl_get);
  602. /**
  603. * geni_se_clk_freq_match() - Get the matching or closest SE clock frequency
  604. * @se: Pointer to the concerned serial engine.
  605. * @req_freq: Requested clock frequency.
  606. * @index: Index of the resultant frequency in the table.
  607. * @res_freq: Resultant frequency of the source clock.
  608. * @exact: Flag to indicate exact multiple requirement of the requested
  609. * frequency.
  610. *
  611. * This function is called by the protocol drivers to determine the best match
  612. * of the requested frequency as provided by the serial engine clock in order
  613. * to meet the performance requirements.
  614. *
  615. * If we return success:
  616. * - if @exact is true then @res_freq / <an_integer> == @req_freq
  617. * - if @exact is false then @res_freq / <an_integer> <= @req_freq
  618. *
  619. * Return: 0 on success, standard Linux error codes on failure.
  620. */
  621. int geni_se_clk_freq_match(struct geni_se *se, unsigned long req_freq,
  622. unsigned int *index, unsigned long *res_freq,
  623. bool exact)
  624. {
  625. unsigned long *tbl;
  626. int num_clk_levels;
  627. int i;
  628. unsigned long best_delta;
  629. unsigned long new_delta;
  630. unsigned int divider;
  631. num_clk_levels = geni_se_clk_tbl_get(se, &tbl);
  632. if (num_clk_levels < 0)
  633. return num_clk_levels;
  634. if (num_clk_levels == 0)
  635. return -EINVAL;
  636. best_delta = ULONG_MAX;
  637. for (i = 0; i < num_clk_levels; i++) {
  638. divider = DIV_ROUND_UP(tbl[i], req_freq);
  639. new_delta = req_freq - tbl[i] / divider;
  640. if (new_delta < best_delta) {
  641. /* We have a new best! */
  642. *index = i;
  643. *res_freq = tbl[i];
  644. /* If the new best is exact then we're done */
  645. if (new_delta == 0)
  646. return 0;
  647. /* Record how close we got */
  648. best_delta = new_delta;
  649. }
  650. }
  651. if (exact)
  652. return -EINVAL;
  653. return 0;
  654. }
  655. EXPORT_SYMBOL_GPL(geni_se_clk_freq_match);
  656. #define GENI_SE_DMA_DONE_EN BIT(0)
  657. #define GENI_SE_DMA_EOT_EN BIT(1)
  658. #define GENI_SE_DMA_AHB_ERR_EN BIT(2)
  659. #define GENI_SE_DMA_RESET_DONE_EN BIT(3)
  660. #define GENI_SE_DMA_FLUSH_DONE BIT(4)
  661. #define GENI_SE_DMA_EOT_BUF BIT(0)
  662. /**
  663. * geni_se_tx_init_dma() - Initiate TX DMA transfer on the serial engine
  664. * @se: Pointer to the concerned serial engine.
  665. * @iova: Mapped DMA address.
  666. * @len: Length of the TX buffer.
  667. *
  668. * This function is used to initiate DMA TX transfer.
  669. */
  670. void geni_se_tx_init_dma(struct geni_se *se, dma_addr_t iova, size_t len)
  671. {
  672. u32 val;
  673. val = GENI_SE_DMA_DONE_EN;
  674. val |= GENI_SE_DMA_EOT_EN;
  675. val |= GENI_SE_DMA_AHB_ERR_EN;
  676. writel_relaxed(val, se->base + SE_DMA_TX_IRQ_EN_SET);
  677. writel_relaxed(lower_32_bits(iova), se->base + SE_DMA_TX_PTR_L);
  678. writel_relaxed(upper_32_bits(iova), se->base + SE_DMA_TX_PTR_H);
  679. writel_relaxed(GENI_SE_DMA_EOT_BUF, se->base + SE_DMA_TX_ATTR);
  680. writel(len, se->base + SE_DMA_TX_LEN);
  681. }
  682. EXPORT_SYMBOL_GPL(geni_se_tx_init_dma);
  683. /**
  684. * geni_se_tx_dma_prep() - Prepare the serial engine for TX DMA transfer
  685. * @se: Pointer to the concerned serial engine.
  686. * @buf: Pointer to the TX buffer.
  687. * @len: Length of the TX buffer.
  688. * @iova: Pointer to store the mapped DMA address.
  689. *
  690. * This function is used to prepare the buffers for DMA TX.
  691. *
  692. * Return: 0 on success, standard Linux error codes on failure.
  693. */
  694. int geni_se_tx_dma_prep(struct geni_se *se, void *buf, size_t len,
  695. dma_addr_t *iova)
  696. {
  697. struct geni_wrapper *wrapper = se->wrapper;
  698. if (!wrapper)
  699. return -EINVAL;
  700. *iova = dma_map_single(wrapper->dev, buf, len, DMA_TO_DEVICE);
  701. if (dma_mapping_error(wrapper->dev, *iova))
  702. return -EIO;
  703. geni_se_tx_init_dma(se, *iova, len);
  704. return 0;
  705. }
  706. EXPORT_SYMBOL_GPL(geni_se_tx_dma_prep);
  707. /**
  708. * geni_se_rx_init_dma() - Initiate RX DMA transfer on the serial engine
  709. * @se: Pointer to the concerned serial engine.
  710. * @iova: Mapped DMA address.
  711. * @len: Length of the RX buffer.
  712. *
  713. * This function is used to initiate DMA RX transfer.
  714. */
  715. void geni_se_rx_init_dma(struct geni_se *se, dma_addr_t iova, size_t len)
  716. {
  717. u32 val;
  718. val = GENI_SE_DMA_DONE_EN;
  719. val |= GENI_SE_DMA_EOT_EN;
  720. val |= GENI_SE_DMA_AHB_ERR_EN;
  721. writel_relaxed(val, se->base + SE_DMA_RX_IRQ_EN_SET);
  722. writel_relaxed(lower_32_bits(iova), se->base + SE_DMA_RX_PTR_L);
  723. writel_relaxed(upper_32_bits(iova), se->base + SE_DMA_RX_PTR_H);
  724. /* RX does not have EOT buffer type bit. So just reset RX_ATTR */
  725. writel_relaxed(0, se->base + SE_DMA_RX_ATTR);
  726. writel(len, se->base + SE_DMA_RX_LEN);
  727. }
  728. EXPORT_SYMBOL_GPL(geni_se_rx_init_dma);
  729. /**
  730. * geni_se_rx_dma_prep() - Prepare the serial engine for RX DMA transfer
  731. * @se: Pointer to the concerned serial engine.
  732. * @buf: Pointer to the RX buffer.
  733. * @len: Length of the RX buffer.
  734. * @iova: Pointer to store the mapped DMA address.
  735. *
  736. * This function is used to prepare the buffers for DMA RX.
  737. *
  738. * Return: 0 on success, standard Linux error codes on failure.
  739. */
  740. int geni_se_rx_dma_prep(struct geni_se *se, void *buf, size_t len,
  741. dma_addr_t *iova)
  742. {
  743. struct geni_wrapper *wrapper = se->wrapper;
  744. if (!wrapper)
  745. return -EINVAL;
  746. *iova = dma_map_single(wrapper->dev, buf, len, DMA_FROM_DEVICE);
  747. if (dma_mapping_error(wrapper->dev, *iova))
  748. return -EIO;
  749. geni_se_rx_init_dma(se, *iova, len);
  750. return 0;
  751. }
  752. EXPORT_SYMBOL_GPL(geni_se_rx_dma_prep);
  753. /**
  754. * geni_se_tx_dma_unprep() - Unprepare the serial engine after TX DMA transfer
  755. * @se: Pointer to the concerned serial engine.
  756. * @iova: DMA address of the TX buffer.
  757. * @len: Length of the TX buffer.
  758. *
  759. * This function is used to unprepare the DMA buffers after DMA TX.
  760. */
  761. void geni_se_tx_dma_unprep(struct geni_se *se, dma_addr_t iova, size_t len)
  762. {
  763. struct geni_wrapper *wrapper = se->wrapper;
  764. if (!dma_mapping_error(wrapper->dev, iova))
  765. dma_unmap_single(wrapper->dev, iova, len, DMA_TO_DEVICE);
  766. }
  767. EXPORT_SYMBOL_GPL(geni_se_tx_dma_unprep);
  768. /**
  769. * geni_se_rx_dma_unprep() - Unprepare the serial engine after RX DMA transfer
  770. * @se: Pointer to the concerned serial engine.
  771. * @iova: DMA address of the RX buffer.
  772. * @len: Length of the RX buffer.
  773. *
  774. * This function is used to unprepare the DMA buffers after DMA RX.
  775. */
  776. void geni_se_rx_dma_unprep(struct geni_se *se, dma_addr_t iova, size_t len)
  777. {
  778. struct geni_wrapper *wrapper = se->wrapper;
  779. if (!dma_mapping_error(wrapper->dev, iova))
  780. dma_unmap_single(wrapper->dev, iova, len, DMA_FROM_DEVICE);
  781. }
  782. EXPORT_SYMBOL_GPL(geni_se_rx_dma_unprep);
  783. int geni_icc_get(struct geni_se *se, const char *icc_ddr)
  784. {
  785. int i, err;
  786. const char *icc_names[] = {"qup-core", "qup-config", icc_ddr};
  787. if (has_acpi_companion(se->dev))
  788. return 0;
  789. for (i = 0; i < ARRAY_SIZE(se->icc_paths); i++) {
  790. if (!icc_names[i])
  791. continue;
  792. se->icc_paths[i].path = devm_of_icc_get(se->dev, icc_names[i]);
  793. if (IS_ERR(se->icc_paths[i].path))
  794. goto err;
  795. }
  796. return 0;
  797. err:
  798. err = PTR_ERR(se->icc_paths[i].path);
  799. if (err != -EPROBE_DEFER)
  800. dev_err_ratelimited(se->dev, "Failed to get ICC path '%s': %d\n",
  801. icc_names[i], err);
  802. return err;
  803. }
  804. EXPORT_SYMBOL_GPL(geni_icc_get);
  805. int geni_icc_set_bw(struct geni_se *se)
  806. {
  807. int i, ret;
  808. for (i = 0; i < ARRAY_SIZE(se->icc_paths); i++) {
  809. ret = icc_set_bw(se->icc_paths[i].path,
  810. se->icc_paths[i].avg_bw, se->icc_paths[i].avg_bw);
  811. if (ret) {
  812. dev_err_ratelimited(se->dev, "ICC BW voting failed on path '%s': %d\n",
  813. icc_path_names[i], ret);
  814. return ret;
  815. }
  816. }
  817. return 0;
  818. }
  819. EXPORT_SYMBOL_GPL(geni_icc_set_bw);
  820. void geni_icc_set_tag(struct geni_se *se, u32 tag)
  821. {
  822. int i;
  823. for (i = 0; i < ARRAY_SIZE(se->icc_paths); i++)
  824. icc_set_tag(se->icc_paths[i].path, tag);
  825. }
  826. EXPORT_SYMBOL_GPL(geni_icc_set_tag);
  827. /* To do: Replace this by icc_bulk_enable once it's implemented in ICC core */
  828. int geni_icc_enable(struct geni_se *se)
  829. {
  830. int i, ret;
  831. for (i = 0; i < ARRAY_SIZE(se->icc_paths); i++) {
  832. ret = icc_enable(se->icc_paths[i].path);
  833. if (ret) {
  834. dev_err_ratelimited(se->dev, "ICC enable failed on path '%s': %d\n",
  835. icc_path_names[i], ret);
  836. return ret;
  837. }
  838. }
  839. return 0;
  840. }
  841. EXPORT_SYMBOL_GPL(geni_icc_enable);
  842. int geni_icc_disable(struct geni_se *se)
  843. {
  844. int i, ret;
  845. for (i = 0; i < ARRAY_SIZE(se->icc_paths); i++) {
  846. ret = icc_disable(se->icc_paths[i].path);
  847. if (ret) {
  848. dev_err_ratelimited(se->dev, "ICC disable failed on path '%s': %d\n",
  849. icc_path_names[i], ret);
  850. return ret;
  851. }
  852. }
  853. return 0;
  854. }
  855. EXPORT_SYMBOL_GPL(geni_icc_disable);
  856. /**
  857. * geni_find_protocol_fw() - Locate and validate SE firmware for a protocol.
  858. * @dev: Pointer to the device structure.
  859. * @fw: Pointer to the firmware image.
  860. * @protocol: Expected serial engine protocol type.
  861. *
  862. * Identifies the appropriate firmware image or configuration required for a
  863. * specific communication protocol instance running on a Qualcomm GENI
  864. * controller.
  865. *
  866. * Return: pointer to a valid 'struct se_fw_hdr' if found, or NULL otherwise.
  867. */
  868. static struct se_fw_hdr *geni_find_protocol_fw(struct device *dev, const struct firmware *fw,
  869. enum geni_se_protocol_type protocol)
  870. {
  871. const struct elf32_hdr *ehdr;
  872. const struct elf32_phdr *phdrs;
  873. const struct elf32_phdr *phdr;
  874. struct se_fw_hdr *sefw;
  875. u32 fw_end, cfg_idx_end, cfg_val_end;
  876. u16 fw_size;
  877. int i;
  878. if (!fw || fw->size < sizeof(struct elf32_hdr))
  879. return NULL;
  880. ehdr = (const struct elf32_hdr *)fw->data;
  881. phdrs = (const struct elf32_phdr *)(fw->data + ehdr->e_phoff);
  882. /*
  883. * The firmware is expected to have at least two program headers (segments).
  884. * One for metadata and the other for the actual protocol-specific firmware.
  885. */
  886. if (ehdr->e_phnum < 2) {
  887. dev_err(dev, "Invalid firmware: less than 2 program headers\n");
  888. return NULL;
  889. }
  890. for (i = 0; i < ehdr->e_phnum; i++) {
  891. phdr = &phdrs[i];
  892. if (fw->size < phdr->p_offset + phdr->p_filesz) {
  893. dev_err(dev, "Firmware size (%zu) < expected offset (%u) + size (%u)\n",
  894. fw->size, phdr->p_offset, phdr->p_filesz);
  895. return NULL;
  896. }
  897. if (phdr->p_type != PT_LOAD || !phdr->p_memsz)
  898. continue;
  899. if (MI_PBT_PAGE_MODE_VALUE(phdr->p_flags) != MI_PBT_NON_PAGED_SEGMENT ||
  900. MI_PBT_SEGMENT_TYPE_VALUE(phdr->p_flags) == MI_PBT_HASH_SEGMENT ||
  901. MI_PBT_ACCESS_TYPE_VALUE(phdr->p_flags) == MI_PBT_NOTUSED_SEGMENT ||
  902. MI_PBT_ACCESS_TYPE_VALUE(phdr->p_flags) == MI_PBT_SHARED_SEGMENT)
  903. continue;
  904. if (phdr->p_filesz < sizeof(struct se_fw_hdr))
  905. continue;
  906. sefw = (struct se_fw_hdr *)(fw->data + phdr->p_offset);
  907. fw_size = le16_to_cpu(sefw->fw_size_in_items);
  908. fw_end = le16_to_cpu(sefw->fw_offset) + fw_size * sizeof(u32);
  909. cfg_idx_end = le16_to_cpu(sefw->cfg_idx_offset) +
  910. le16_to_cpu(sefw->cfg_size_in_items) * sizeof(u8);
  911. cfg_val_end = le16_to_cpu(sefw->cfg_val_offset) +
  912. le16_to_cpu(sefw->cfg_size_in_items) * sizeof(u32);
  913. if (le32_to_cpu(sefw->magic) != SE_MAGIC_NUM || le32_to_cpu(sefw->version) != 1)
  914. continue;
  915. if (le32_to_cpu(sefw->serial_protocol) != protocol)
  916. continue;
  917. if (fw_size % 2 != 0) {
  918. fw_size++;
  919. sefw->fw_size_in_items = cpu_to_le16(fw_size);
  920. }
  921. if (fw_size >= MAX_GENI_CFG_RAMn_CNT) {
  922. dev_err(dev,
  923. "Firmware size (%u) exceeds max allowed RAMn count (%u)\n",
  924. fw_size, MAX_GENI_CFG_RAMn_CNT);
  925. continue;
  926. }
  927. if (fw_end > phdr->p_filesz || cfg_idx_end > phdr->p_filesz ||
  928. cfg_val_end > phdr->p_filesz) {
  929. dev_err(dev, "Truncated or corrupt SE FW segment found at index %d\n", i);
  930. continue;
  931. }
  932. return sefw;
  933. }
  934. dev_err(dev, "Failed to get %s protocol firmware\n", protocol_name[protocol]);
  935. return NULL;
  936. }
  937. /**
  938. * geni_configure_xfer_mode() - Set the transfer mode.
  939. * @se: Pointer to the concerned serial engine.
  940. * @mode: SE data transfer mode.
  941. *
  942. * Set the transfer mode to either FIFO or DMA according to the mode specified
  943. * by the protocol driver.
  944. *
  945. * Return: 0 if successful, otherwise return an error value.
  946. */
  947. static int geni_configure_xfer_mode(struct geni_se *se, enum geni_se_xfer_mode mode)
  948. {
  949. /* Configure SE FIFO, DMA or GSI mode. */
  950. switch (mode) {
  951. case GENI_GPI_DMA:
  952. geni_setbits32(se->base + SE_GENI_DMA_MODE_EN, GENI_DMA_MODE_EN);
  953. writel(0x0, se->base + SE_IRQ_EN);
  954. writel(DMA_RX_EVENT_EN | DMA_TX_EVENT_EN | GENI_M_EVENT_EN | GENI_S_EVENT_EN,
  955. se->base + SE_GSI_EVENT_EN);
  956. break;
  957. case GENI_SE_FIFO:
  958. geni_clrbits32(se->base + SE_GENI_DMA_MODE_EN, GENI_DMA_MODE_EN);
  959. writel(DMA_RX_IRQ_EN | DMA_TX_IRQ_EN | GENI_M_IRQ_EN | GENI_S_IRQ_EN,
  960. se->base + SE_IRQ_EN);
  961. writel(0x0, se->base + SE_GSI_EVENT_EN);
  962. break;
  963. case GENI_SE_DMA:
  964. geni_setbits32(se->base + SE_GENI_DMA_MODE_EN, GENI_DMA_MODE_EN);
  965. writel(DMA_RX_IRQ_EN | DMA_TX_IRQ_EN | GENI_M_IRQ_EN | GENI_S_IRQ_EN,
  966. se->base + SE_IRQ_EN);
  967. writel(0x0, se->base + SE_GSI_EVENT_EN);
  968. break;
  969. default:
  970. dev_err(se->dev, "Invalid geni-se transfer mode: %d\n", mode);
  971. return -EINVAL;
  972. }
  973. return 0;
  974. }
  975. /**
  976. * geni_enable_interrupts() - Enable interrupts.
  977. * @se: Pointer to the concerned serial engine.
  978. *
  979. * Enable the required interrupts during the firmware load process.
  980. */
  981. static void geni_enable_interrupts(struct geni_se *se)
  982. {
  983. u32 val;
  984. /* Enable required interrupts. */
  985. writel(M_COMMON_GENI_M_IRQ_EN, se->base + SE_GENI_M_IRQ_EN);
  986. val = S_CMD_OVERRUN_EN | S_ILLEGAL_CMD_EN | S_CMD_CANCEL_EN | S_CMD_ABORT_EN |
  987. S_GP_IRQ_0_EN | S_GP_IRQ_1_EN | S_GP_IRQ_2_EN | S_GP_IRQ_3_EN |
  988. S_RX_FIFO_WR_ERR_EN | S_RX_FIFO_RD_ERR_EN;
  989. writel(val, se->base + SE_GENI_S_IRQ_ENABLE);
  990. /* DMA mode configuration. */
  991. val = GENI_SE_DMA_RESET_DONE_EN | GENI_SE_DMA_AHB_ERR_EN | GENI_SE_DMA_DONE_EN;
  992. writel(val, se->base + SE_DMA_TX_IRQ_EN_SET);
  993. val = GENI_SE_DMA_FLUSH_DONE | GENI_SE_DMA_RESET_DONE_EN | GENI_SE_DMA_AHB_ERR_EN |
  994. GENI_SE_DMA_DONE_EN;
  995. writel(val, se->base + SE_DMA_RX_IRQ_EN_SET);
  996. }
  997. /**
  998. * geni_write_fw_revision() - Write the firmware revision.
  999. * @se: Pointer to the concerned serial engine.
  1000. * @serial_protocol: serial protocol type.
  1001. * @fw_version: QUP firmware version.
  1002. *
  1003. * Write the firmware revision and protocol into the respective register.
  1004. */
  1005. static void geni_write_fw_revision(struct geni_se *se, u16 serial_protocol, u16 fw_version)
  1006. {
  1007. u32 reg;
  1008. reg = FIELD_PREP(FW_REV_PROTOCOL_MSK, serial_protocol);
  1009. reg |= FIELD_PREP(FW_REV_VERSION_MSK, fw_version);
  1010. writel(reg, se->base + SE_GENI_FW_REVISION);
  1011. writel(reg, se->base + SE_GENI_S_FW_REVISION);
  1012. }
  1013. /**
  1014. * geni_load_se_fw() - Load Serial Engine specific firmware.
  1015. * @se: Pointer to the concerned serial engine.
  1016. * @fw: Pointer to the firmware structure.
  1017. * @mode: SE data transfer mode.
  1018. * @protocol: Protocol type to be used with the SE (e.g., UART, SPI, I2C).
  1019. *
  1020. * Load the protocol firmware into the IRAM of the Serial Engine.
  1021. *
  1022. * Return: 0 if successful, otherwise return an error value.
  1023. */
  1024. static int geni_load_se_fw(struct geni_se *se, const struct firmware *fw,
  1025. enum geni_se_xfer_mode mode, enum geni_se_protocol_type protocol)
  1026. {
  1027. const u32 *fw_data, *cfg_val_arr;
  1028. const u8 *cfg_idx_arr;
  1029. u32 i, reg_value;
  1030. int ret;
  1031. struct se_fw_hdr *hdr;
  1032. hdr = geni_find_protocol_fw(se->dev, fw, protocol);
  1033. if (!hdr)
  1034. return -EINVAL;
  1035. fw_data = (const u32 *)((u8 *)hdr + le16_to_cpu(hdr->fw_offset));
  1036. cfg_idx_arr = (const u8 *)hdr + le16_to_cpu(hdr->cfg_idx_offset);
  1037. cfg_val_arr = (const u32 *)((u8 *)hdr + le16_to_cpu(hdr->cfg_val_offset));
  1038. ret = geni_icc_set_bw(se);
  1039. if (ret)
  1040. return ret;
  1041. ret = geni_icc_enable(se);
  1042. if (ret)
  1043. return ret;
  1044. ret = geni_se_resources_on(se);
  1045. if (ret)
  1046. goto out_icc_disable;
  1047. /*
  1048. * Disable high-priority interrupts until all currently executing
  1049. * low-priority interrupts have been fully handled.
  1050. */
  1051. geni_setbits32(se->wrapper->base + QUPV3_COMMON_CFG, FAST_SWITCH_TO_HIGH_DISABLE);
  1052. /* Set AHB_M_CLK_CGC_ON to indicate hardware controls se-wrapper cgc clock. */
  1053. geni_setbits32(se->wrapper->base + QUPV3_SE_AHB_M_CFG, AHB_M_CLK_CGC_ON);
  1054. /* Let hardware to control common cgc. */
  1055. geni_setbits32(se->wrapper->base + QUPV3_COMMON_CGC_CTRL, COMMON_CSR_SLV_CLK_CGC_ON);
  1056. /*
  1057. * Setting individual bits in GENI_OUTPUT_CTRL activates corresponding output lines,
  1058. * allowing the hardware to drive data as configured.
  1059. */
  1060. writel(0x0, se->base + GENI_OUTPUT_CTRL);
  1061. /* Set SCLK and HCLK to program RAM */
  1062. geni_setbits32(se->base + SE_GENI_CGC_CTRL, PROG_RAM_SCLK_OFF | PROG_RAM_HCLK_OFF);
  1063. writel(0x0, se->base + SE_GENI_CLK_CTRL);
  1064. geni_clrbits32(se->base + SE_GENI_CGC_CTRL, PROG_RAM_SCLK_OFF | PROG_RAM_HCLK_OFF);
  1065. /* Enable required clocks for DMA CSR, TX and RX. */
  1066. reg_value = AHB_SEC_SLV_CLK_CGC_ON | DMA_AHB_SLV_CLK_CGC_ON |
  1067. DMA_TX_CLK_CGC_ON | DMA_RX_CLK_CGC_ON;
  1068. geni_setbits32(se->base + SE_DMA_GENERAL_CFG, reg_value);
  1069. /* Let hardware control CGC by default. */
  1070. writel(DEFAULT_CGC_EN, se->base + SE_GENI_CGC_CTRL);
  1071. /* Set version of the configuration register part of firmware. */
  1072. writel(le16_to_cpu(hdr->cfg_version), se->base + SE_GENI_INIT_CFG_REVISION);
  1073. writel(le16_to_cpu(hdr->cfg_version), se->base + SE_GENI_S_INIT_CFG_REVISION);
  1074. /* Configure GENI primitive table. */
  1075. for (i = 0; i < le16_to_cpu(hdr->cfg_size_in_items); i++)
  1076. writel(cfg_val_arr[i],
  1077. se->base + SE_GENI_CFG_REG0 + (cfg_idx_arr[i] * sizeof(u32)));
  1078. /* Configure condition for assertion of RX_RFR_WATERMARK condition. */
  1079. reg_value = geni_se_get_rx_fifo_depth(se);
  1080. writel(reg_value - 2, se->base + SE_GENI_RX_RFR_WATERMARK_REG);
  1081. /* Let hardware control CGC */
  1082. geni_setbits32(se->base + GENI_OUTPUT_CTRL, DEFAULT_IO_OUTPUT_CTRL_MSK);
  1083. ret = geni_configure_xfer_mode(se, mode);
  1084. if (ret)
  1085. goto out_resources_off;
  1086. geni_enable_interrupts(se);
  1087. geni_write_fw_revision(se, le16_to_cpu(hdr->serial_protocol), le16_to_cpu(hdr->fw_version));
  1088. /* Program RAM address space. */
  1089. memcpy_toio(se->base + SE_GENI_CFG_RAMN, fw_data,
  1090. le16_to_cpu(hdr->fw_size_in_items) * sizeof(u32));
  1091. /* Put default values on GENI's output pads. */
  1092. writel_relaxed(0x1, se->base + GENI_FORCE_DEFAULT_REG);
  1093. /* Toggle SCLK/HCLK from high to low to finalize RAM programming and apply config. */
  1094. geni_setbits32(se->base + SE_GENI_CGC_CTRL, PROG_RAM_SCLK_OFF | PROG_RAM_HCLK_OFF);
  1095. geni_setbits32(se->base + SE_GENI_CLK_CTRL, SER_CLK_SEL);
  1096. geni_clrbits32(se->base + SE_GENI_CGC_CTRL, PROG_RAM_SCLK_OFF | PROG_RAM_HCLK_OFF);
  1097. /* Serial engine DMA interface is enabled. */
  1098. geni_setbits32(se->base + SE_DMA_IF_EN, DMA_IF_EN);
  1099. /* Enable or disable FIFO interface of the serial engine. */
  1100. if (mode == GENI_SE_FIFO)
  1101. geni_clrbits32(se->base + SE_FIFO_IF_DISABLE, FIFO_IF_DISABLE);
  1102. else
  1103. geni_setbits32(se->base + SE_FIFO_IF_DISABLE, FIFO_IF_DISABLE);
  1104. out_resources_off:
  1105. geni_se_resources_off(se);
  1106. out_icc_disable:
  1107. geni_icc_disable(se);
  1108. return ret;
  1109. }
  1110. /**
  1111. * geni_load_se_firmware() - Load firmware for SE based on protocol
  1112. * @se: Pointer to the concerned serial engine.
  1113. * @protocol: Protocol type to be used with the SE (e.g., UART, SPI, I2C).
  1114. *
  1115. * Retrieves the firmware name from device properties and sets the transfer mode
  1116. * (FIFO or GSI DMA) based on device tree configuration. Enforces FIFO mode for
  1117. * UART protocol due to lack of GSI DMA support. Requests the firmware and loads
  1118. * it into the SE.
  1119. *
  1120. * Return: 0 on success, negative error code on failure.
  1121. */
  1122. int geni_load_se_firmware(struct geni_se *se, enum geni_se_protocol_type protocol)
  1123. {
  1124. const char *fw_name;
  1125. const struct firmware *fw;
  1126. enum geni_se_xfer_mode mode = GENI_SE_FIFO;
  1127. int ret;
  1128. if (protocol >= ARRAY_SIZE(protocol_name)) {
  1129. dev_err(se->dev, "Invalid geni-se protocol: %d", protocol);
  1130. return -EINVAL;
  1131. }
  1132. ret = device_property_read_string(se->wrapper->dev, "firmware-name", &fw_name);
  1133. if (ret) {
  1134. dev_err(se->dev, "Failed to read firmware-name property: %d\n", ret);
  1135. return -EINVAL;
  1136. }
  1137. if (of_property_read_bool(se->dev->of_node, "qcom,enable-gsi-dma"))
  1138. mode = GENI_GPI_DMA;
  1139. /* GSI mode is not supported by the UART driver; therefore, setting FIFO mode */
  1140. if (protocol == GENI_SE_UART)
  1141. mode = GENI_SE_FIFO;
  1142. ret = request_firmware(&fw, fw_name, se->dev);
  1143. if (ret) {
  1144. if (ret == -ENOENT)
  1145. return -EPROBE_DEFER;
  1146. dev_err(se->dev, "Failed to request firmware '%s' for protocol %d: ret: %d\n",
  1147. fw_name, protocol, ret);
  1148. return ret;
  1149. }
  1150. ret = geni_load_se_fw(se, fw, mode, protocol);
  1151. release_firmware(fw);
  1152. if (ret) {
  1153. dev_err(se->dev, "Failed to load SE firmware for protocol %d: ret: %d\n",
  1154. protocol, ret);
  1155. return ret;
  1156. }
  1157. dev_dbg(se->dev, "Firmware load for %s protocol is successful for xfer mode: %d\n",
  1158. protocol_name[protocol], mode);
  1159. return 0;
  1160. }
  1161. EXPORT_SYMBOL_GPL(geni_load_se_firmware);
  1162. static int geni_se_probe(struct platform_device *pdev)
  1163. {
  1164. struct device *dev = &pdev->dev;
  1165. struct geni_wrapper *wrapper;
  1166. const struct geni_se_desc *desc;
  1167. int ret;
  1168. wrapper = devm_kzalloc(dev, sizeof(*wrapper), GFP_KERNEL);
  1169. if (!wrapper)
  1170. return -ENOMEM;
  1171. wrapper->dev = dev;
  1172. wrapper->base = devm_platform_ioremap_resource(pdev, 0);
  1173. if (IS_ERR(wrapper->base))
  1174. return PTR_ERR(wrapper->base);
  1175. desc = device_get_match_data(&pdev->dev);
  1176. if (!has_acpi_companion(&pdev->dev) && desc->num_clks) {
  1177. int i;
  1178. wrapper->num_clks = min_t(unsigned int, desc->num_clks, MAX_CLKS);
  1179. for (i = 0; i < wrapper->num_clks; ++i)
  1180. wrapper->clks[i].id = desc->clks[i];
  1181. ret = of_count_phandle_with_args(dev->of_node, "clocks", "#clock-cells");
  1182. if (ret < 0) {
  1183. dev_err(dev, "invalid clocks property at %pOF\n", dev->of_node);
  1184. return ret;
  1185. }
  1186. if (ret < wrapper->num_clks) {
  1187. dev_err(dev, "invalid clocks count at %pOF, expected %d entries\n",
  1188. dev->of_node, wrapper->num_clks);
  1189. return -EINVAL;
  1190. }
  1191. ret = devm_clk_bulk_get(dev, wrapper->num_clks, wrapper->clks);
  1192. if (ret) {
  1193. dev_err(dev, "Err getting clks %d\n", ret);
  1194. return ret;
  1195. }
  1196. }
  1197. dev_set_drvdata(dev, wrapper);
  1198. dev_dbg(dev, "GENI SE Driver probed\n");
  1199. return devm_of_platform_populate(dev);
  1200. }
  1201. static const char * const qup_clks[] = {
  1202. "m-ahb",
  1203. "s-ahb",
  1204. };
  1205. static const struct geni_se_desc qup_desc = {
  1206. .clks = qup_clks,
  1207. .num_clks = ARRAY_SIZE(qup_clks),
  1208. };
  1209. static const struct geni_se_desc sa8255p_qup_desc = {};
  1210. static const char * const i2c_master_hub_clks[] = {
  1211. "s-ahb",
  1212. };
  1213. static const struct geni_se_desc i2c_master_hub_desc = {
  1214. .clks = i2c_master_hub_clks,
  1215. .num_clks = ARRAY_SIZE(i2c_master_hub_clks),
  1216. };
  1217. static const struct of_device_id geni_se_dt_match[] = {
  1218. { .compatible = "qcom,geni-se-qup", .data = &qup_desc },
  1219. { .compatible = "qcom,geni-se-i2c-master-hub", .data = &i2c_master_hub_desc },
  1220. { .compatible = "qcom,sa8255p-geni-se-qup", .data = &sa8255p_qup_desc },
  1221. {}
  1222. };
  1223. MODULE_DEVICE_TABLE(of, geni_se_dt_match);
  1224. static struct platform_driver geni_se_driver = {
  1225. .driver = {
  1226. .name = "geni_se_qup",
  1227. .of_match_table = geni_se_dt_match,
  1228. },
  1229. .probe = geni_se_probe,
  1230. };
  1231. module_platform_driver(geni_se_driver);
  1232. MODULE_DESCRIPTION("GENI Serial Engine Driver");
  1233. MODULE_LICENSE("GPL v2");