fman_port.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. /*
  2. * Copyright 2008 - 2015 Freescale Semiconductor Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of Freescale Semiconductor nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. *
  16. * ALTERNATIVELY, this software may be distributed under the terms of the
  17. * GNU General Public License ("GPL") as published by the Free Software
  18. * Foundation, either version 2 of that License or (at your option) any
  19. * later version.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
  22. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
  25. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include "fman_port.h"
  34. #include "fman.h"
  35. #include "fman_sp.h"
  36. #include <linux/io.h>
  37. #include <linux/slab.h>
  38. #include <linux/module.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/of_platform.h>
  41. #include <linux/of_address.h>
  42. #include <linux/delay.h>
  43. #include <linux/libfdt_env.h>
  44. /* Queue ID */
  45. #define DFLT_FQ_ID 0x00FFFFFF
  46. /* General defines */
  47. #define PORT_BMI_FIFO_UNITS 0x100
  48. #define MAX_PORT_FIFO_SIZE(bmi_max_fifo_size) \
  49. min((u32)bmi_max_fifo_size, (u32)1024 * FMAN_BMI_FIFO_UNITS)
  50. #define PORT_CG_MAP_NUM 8
  51. #define PORT_PRS_RESULT_WORDS_NUM 8
  52. #define PORT_IC_OFFSET_UNITS 0x10
  53. #define MIN_EXT_BUF_SIZE 64
  54. #define BMI_PORT_REGS_OFFSET 0
  55. #define QMI_PORT_REGS_OFFSET 0x400
  56. #define HWP_PORT_REGS_OFFSET 0x800
  57. /* Default values */
  58. #define DFLT_PORT_BUFFER_PREFIX_CONTEXT_DATA_ALIGN \
  59. DFLT_FM_SP_BUFFER_PREFIX_CONTEXT_DATA_ALIGN
  60. #define DFLT_PORT_CUT_BYTES_FROM_END 4
  61. #define DFLT_PORT_ERRORS_TO_DISCARD FM_PORT_FRM_ERR_CLS_DISCARD
  62. #define DFLT_PORT_MAX_FRAME_LENGTH 9600
  63. #define DFLT_PORT_RX_FIFO_PRI_ELEVATION_LEV(bmi_max_fifo_size) \
  64. MAX_PORT_FIFO_SIZE(bmi_max_fifo_size)
  65. #define DFLT_PORT_RX_FIFO_THRESHOLD(major, bmi_max_fifo_size) \
  66. (major == 6 ? \
  67. MAX_PORT_FIFO_SIZE(bmi_max_fifo_size) : \
  68. (MAX_PORT_FIFO_SIZE(bmi_max_fifo_size) * 3 / 4)) \
  69. #define DFLT_PORT_EXTRA_NUM_OF_FIFO_BUFS 0
  70. /* QMI defines */
  71. #define QMI_DEQ_CFG_SUBPORTAL_MASK 0x1f
  72. #define QMI_PORT_CFG_EN 0x80000000
  73. #define QMI_PORT_STATUS_DEQ_FD_BSY 0x20000000
  74. #define QMI_DEQ_CFG_PRI 0x80000000
  75. #define QMI_DEQ_CFG_TYPE1 0x10000000
  76. #define QMI_DEQ_CFG_TYPE2 0x20000000
  77. #define QMI_DEQ_CFG_TYPE3 0x30000000
  78. #define QMI_DEQ_CFG_PREFETCH_PARTIAL 0x01000000
  79. #define QMI_DEQ_CFG_PREFETCH_FULL 0x03000000
  80. #define QMI_DEQ_CFG_SP_MASK 0xf
  81. #define QMI_DEQ_CFG_SP_SHIFT 20
  82. #define QMI_BYTE_COUNT_LEVEL_CONTROL(_type) \
  83. (_type == FMAN_PORT_TYPE_TX ? 0x1400 : 0x400)
  84. /* BMI defins */
  85. #define BMI_EBD_EN 0x80000000
  86. #define BMI_PORT_CFG_EN 0x80000000
  87. #define BMI_PORT_STATUS_BSY 0x80000000
  88. #define BMI_DMA_ATTR_SWP_SHIFT FMAN_SP_DMA_ATTR_SWP_SHIFT
  89. #define BMI_DMA_ATTR_WRITE_OPTIMIZE FMAN_SP_DMA_ATTR_WRITE_OPTIMIZE
  90. #define BMI_RX_FIFO_PRI_ELEVATION_SHIFT 16
  91. #define BMI_RX_FIFO_THRESHOLD_ETHE 0x80000000
  92. #define BMI_FRAME_END_CS_IGNORE_SHIFT 24
  93. #define BMI_FRAME_END_CS_IGNORE_MASK 0x0000001f
  94. #define BMI_RX_FRAME_END_CUT_SHIFT 16
  95. #define BMI_RX_FRAME_END_CUT_MASK 0x0000001f
  96. #define BMI_IC_TO_EXT_SHIFT FMAN_SP_IC_TO_EXT_SHIFT
  97. #define BMI_IC_TO_EXT_MASK 0x0000001f
  98. #define BMI_IC_FROM_INT_SHIFT FMAN_SP_IC_FROM_INT_SHIFT
  99. #define BMI_IC_FROM_INT_MASK 0x0000000f
  100. #define BMI_IC_SIZE_MASK 0x0000001f
  101. #define BMI_INT_BUF_MARG_SHIFT 28
  102. #define BMI_INT_BUF_MARG_MASK 0x0000000f
  103. #define BMI_EXT_BUF_MARG_START_SHIFT FMAN_SP_EXT_BUF_MARG_START_SHIFT
  104. #define BMI_EXT_BUF_MARG_START_MASK 0x000001ff
  105. #define BMI_EXT_BUF_MARG_END_MASK 0x000001ff
  106. #define BMI_CMD_MR_LEAC 0x00200000
  107. #define BMI_CMD_MR_SLEAC 0x00100000
  108. #define BMI_CMD_MR_MA 0x00080000
  109. #define BMI_CMD_MR_DEAS 0x00040000
  110. #define BMI_CMD_RX_MR_DEF (BMI_CMD_MR_LEAC | \
  111. BMI_CMD_MR_SLEAC | \
  112. BMI_CMD_MR_MA | \
  113. BMI_CMD_MR_DEAS)
  114. #define BMI_CMD_TX_MR_DEF 0
  115. #define BMI_CMD_ATTR_ORDER 0x80000000
  116. #define BMI_CMD_ATTR_SYNC 0x02000000
  117. #define BMI_CMD_ATTR_COLOR_SHIFT 26
  118. #define BMI_FIFO_PIPELINE_DEPTH_SHIFT 12
  119. #define BMI_FIFO_PIPELINE_DEPTH_MASK 0x0000000f
  120. #define BMI_NEXT_ENG_FD_BITS_SHIFT 24
  121. #define BMI_EXT_BUF_POOL_VALID FMAN_SP_EXT_BUF_POOL_VALID
  122. #define BMI_EXT_BUF_POOL_EN_COUNTER FMAN_SP_EXT_BUF_POOL_EN_COUNTER
  123. #define BMI_EXT_BUF_POOL_BACKUP FMAN_SP_EXT_BUF_POOL_BACKUP
  124. #define BMI_EXT_BUF_POOL_ID_SHIFT 16
  125. #define BMI_EXT_BUF_POOL_ID_MASK 0x003F0000
  126. #define BMI_POOL_DEP_NUM_OF_POOLS_SHIFT 16
  127. #define BMI_TX_FIFO_MIN_FILL_SHIFT 16
  128. #define BMI_PRIORITY_ELEVATION_LEVEL ((0x3FF + 1) * PORT_BMI_FIFO_UNITS)
  129. #define BMI_FIFO_THRESHOLD ((0x3FF + 1) * PORT_BMI_FIFO_UNITS)
  130. #define BMI_DEQUEUE_PIPELINE_DEPTH(_type, _speed) \
  131. ((_type == FMAN_PORT_TYPE_TX && _speed == 10000) ? 4 : 1)
  132. #define RX_ERRS_TO_ENQ \
  133. (FM_PORT_FRM_ERR_DMA | \
  134. FM_PORT_FRM_ERR_PHYSICAL | \
  135. FM_PORT_FRM_ERR_SIZE | \
  136. FM_PORT_FRM_ERR_EXTRACTION | \
  137. FM_PORT_FRM_ERR_NO_SCHEME | \
  138. FM_PORT_FRM_ERR_PRS_TIMEOUT | \
  139. FM_PORT_FRM_ERR_PRS_ILL_INSTRUCT | \
  140. FM_PORT_FRM_ERR_BLOCK_LIMIT_EXCEEDED | \
  141. FM_PORT_FRM_ERR_PRS_HDR_ERR | \
  142. FM_PORT_FRM_ERR_KEYSIZE_OVERFLOW | \
  143. FM_PORT_FRM_ERR_IPRE)
  144. /* NIA defines */
  145. #define NIA_ORDER_RESTOR 0x00800000
  146. #define NIA_ENG_BMI 0x00500000
  147. #define NIA_ENG_QMI_ENQ 0x00540000
  148. #define NIA_ENG_QMI_DEQ 0x00580000
  149. #define NIA_ENG_HWP 0x00440000
  150. #define NIA_BMI_AC_ENQ_FRAME 0x00000002
  151. #define NIA_BMI_AC_TX_RELEASE 0x000002C0
  152. #define NIA_BMI_AC_RELEASE 0x000000C0
  153. #define NIA_BMI_AC_TX 0x00000274
  154. #define NIA_BMI_AC_FETCH_ALL_FRAME 0x0000020c
  155. /* Port IDs */
  156. #define TX_10G_PORT_BASE 0x30
  157. #define RX_10G_PORT_BASE 0x10
  158. /* BMI Rx port register map */
  159. struct fman_port_rx_bmi_regs {
  160. u32 fmbm_rcfg; /* Rx Configuration */
  161. u32 fmbm_rst; /* Rx Status */
  162. u32 fmbm_rda; /* Rx DMA attributes */
  163. u32 fmbm_rfp; /* Rx FIFO Parameters */
  164. u32 fmbm_rfed; /* Rx Frame End Data */
  165. u32 fmbm_ricp; /* Rx Internal Context Parameters */
  166. u32 fmbm_rim; /* Rx Internal Buffer Margins */
  167. u32 fmbm_rebm; /* Rx External Buffer Margins */
  168. u32 fmbm_rfne; /* Rx Frame Next Engine */
  169. u32 fmbm_rfca; /* Rx Frame Command Attributes. */
  170. u32 fmbm_rfpne; /* Rx Frame Parser Next Engine */
  171. u32 fmbm_rpso; /* Rx Parse Start Offset */
  172. u32 fmbm_rpp; /* Rx Policer Profile */
  173. u32 fmbm_rccb; /* Rx Coarse Classification Base */
  174. u32 fmbm_reth; /* Rx Excessive Threshold */
  175. u32 reserved003c[1]; /* (0x03C 0x03F) */
  176. u32 fmbm_rprai[PORT_PRS_RESULT_WORDS_NUM];
  177. /* Rx Parse Results Array Init */
  178. u32 fmbm_rfqid; /* Rx Frame Queue ID */
  179. u32 fmbm_refqid; /* Rx Error Frame Queue ID */
  180. u32 fmbm_rfsdm; /* Rx Frame Status Discard Mask */
  181. u32 fmbm_rfsem; /* Rx Frame Status Error Mask */
  182. u32 fmbm_rfene; /* Rx Frame Enqueue Next Engine */
  183. u32 reserved0074[0x2]; /* (0x074-0x07C) */
  184. u32 fmbm_rcmne; /* Rx Frame Continuous Mode Next Engine */
  185. u32 reserved0080[0x20]; /* (0x080 0x0FF) */
  186. u32 fmbm_ebmpi[FMAN_PORT_MAX_EXT_POOLS_NUM];
  187. /* Buffer Manager pool Information- */
  188. u32 fmbm_acnt[FMAN_PORT_MAX_EXT_POOLS_NUM]; /* Allocate Counter- */
  189. u32 reserved0130[8]; /* 0x130/0x140 - 0x15F reserved - */
  190. u32 fmbm_rcgm[PORT_CG_MAP_NUM]; /* Congestion Group Map */
  191. u32 fmbm_mpd; /* BM Pool Depletion */
  192. u32 reserved0184[0x1F]; /* (0x184 0x1FF) */
  193. u32 fmbm_rstc; /* Rx Statistics Counters */
  194. u32 fmbm_rfrc; /* Rx Frame Counter */
  195. u32 fmbm_rfbc; /* Rx Bad Frames Counter */
  196. u32 fmbm_rlfc; /* Rx Large Frames Counter */
  197. u32 fmbm_rffc; /* Rx Filter Frames Counter */
  198. u32 fmbm_rfdc; /* Rx Frame Discard Counter */
  199. u32 fmbm_rfldec; /* Rx Frames List DMA Error Counter */
  200. u32 fmbm_rodc; /* Rx Out of Buffers Discard nntr */
  201. u32 fmbm_rbdc; /* Rx Buffers Deallocate Counter */
  202. u32 fmbm_rpec; /* RX Prepare to enqueue Counte */
  203. u32 reserved0224[0x16]; /* (0x224 0x27F) */
  204. u32 fmbm_rpc; /* Rx Performance Counters */
  205. u32 fmbm_rpcp; /* Rx Performance Count Parameters */
  206. u32 fmbm_rccn; /* Rx Cycle Counter */
  207. u32 fmbm_rtuc; /* Rx Tasks Utilization Counter */
  208. u32 fmbm_rrquc; /* Rx Receive Queue Utilization cntr */
  209. u32 fmbm_rduc; /* Rx DMA Utilization Counter */
  210. u32 fmbm_rfuc; /* Rx FIFO Utilization Counter */
  211. u32 fmbm_rpac; /* Rx Pause Activation Counter */
  212. u32 reserved02a0[0x18]; /* (0x2A0 0x2FF) */
  213. u32 fmbm_rdcfg[0x3]; /* Rx Debug Configuration */
  214. u32 fmbm_rgpr; /* Rx General Purpose Register */
  215. u32 reserved0310[0x3a];
  216. };
  217. /* BMI Tx port register map */
  218. struct fman_port_tx_bmi_regs {
  219. u32 fmbm_tcfg; /* Tx Configuration */
  220. u32 fmbm_tst; /* Tx Status */
  221. u32 fmbm_tda; /* Tx DMA attributes */
  222. u32 fmbm_tfp; /* Tx FIFO Parameters */
  223. u32 fmbm_tfed; /* Tx Frame End Data */
  224. u32 fmbm_ticp; /* Tx Internal Context Parameters */
  225. u32 fmbm_tfdne; /* Tx Frame Dequeue Next Engine. */
  226. u32 fmbm_tfca; /* Tx Frame Command attribute. */
  227. u32 fmbm_tcfqid; /* Tx Confirmation Frame Queue ID. */
  228. u32 fmbm_tefqid; /* Tx Frame Error Queue ID */
  229. u32 fmbm_tfene; /* Tx Frame Enqueue Next Engine */
  230. u32 fmbm_trlmts; /* Tx Rate Limiter Scale */
  231. u32 fmbm_trlmt; /* Tx Rate Limiter */
  232. u32 reserved0034[0x0e]; /* (0x034-0x6c) */
  233. u32 fmbm_tccb; /* Tx Coarse Classification base */
  234. u32 fmbm_tfne; /* Tx Frame Next Engine */
  235. u32 fmbm_tpfcm[0x02];
  236. /* Tx Priority based Flow Control (PFC) Mapping */
  237. u32 fmbm_tcmne; /* Tx Frame Continuous Mode Next Engine */
  238. u32 reserved0080[0x60]; /* (0x080-0x200) */
  239. u32 fmbm_tstc; /* Tx Statistics Counters */
  240. u32 fmbm_tfrc; /* Tx Frame Counter */
  241. u32 fmbm_tfdc; /* Tx Frames Discard Counter */
  242. u32 fmbm_tfledc; /* Tx Frame len error discard cntr */
  243. u32 fmbm_tfufdc; /* Tx Frame unsprt frmt discard cntr */
  244. u32 fmbm_tbdc; /* Tx Buffers Deallocate Counter */
  245. u32 reserved0218[0x1A]; /* (0x218-0x280) */
  246. u32 fmbm_tpc; /* Tx Performance Counters */
  247. u32 fmbm_tpcp; /* Tx Performance Count Parameters */
  248. u32 fmbm_tccn; /* Tx Cycle Counter */
  249. u32 fmbm_ttuc; /* Tx Tasks Utilization Counter */
  250. u32 fmbm_ttcquc; /* Tx Transmit conf Q util Counter */
  251. u32 fmbm_tduc; /* Tx DMA Utilization Counter */
  252. u32 fmbm_tfuc; /* Tx FIFO Utilization Counter */
  253. u32 reserved029c[16]; /* (0x29C-0x2FF) */
  254. u32 fmbm_tdcfg[0x3]; /* Tx Debug Configuration */
  255. u32 fmbm_tgpr; /* Tx General Purpose Register */
  256. u32 reserved0310[0x3a]; /* (0x310-0x3FF) */
  257. };
  258. /* BMI port register map */
  259. union fman_port_bmi_regs {
  260. struct fman_port_rx_bmi_regs rx;
  261. struct fman_port_tx_bmi_regs tx;
  262. };
  263. /* QMI port register map */
  264. struct fman_port_qmi_regs {
  265. u32 fmqm_pnc; /* PortID n Configuration Register */
  266. u32 fmqm_pns; /* PortID n Status Register */
  267. u32 fmqm_pnts; /* PortID n Task Status Register */
  268. u32 reserved00c[4]; /* 0xn00C - 0xn01B */
  269. u32 fmqm_pnen; /* PortID n Enqueue NIA Register */
  270. u32 fmqm_pnetfc; /* PortID n Enq Total Frame Counter */
  271. u32 reserved024[2]; /* 0xn024 - 0x02B */
  272. u32 fmqm_pndn; /* PortID n Dequeue NIA Register */
  273. u32 fmqm_pndc; /* PortID n Dequeue Config Register */
  274. u32 fmqm_pndtfc; /* PortID n Dequeue tot Frame cntr */
  275. u32 fmqm_pndfdc; /* PortID n Dequeue FQID Dflt Cntr */
  276. u32 fmqm_pndcc; /* PortID n Dequeue Confirm Counter */
  277. };
  278. #define HWP_HXS_COUNT 16
  279. #define HWP_HXS_PHE_REPORT 0x00000800
  280. #define HWP_HXS_PCAC_PSTAT 0x00000100
  281. #define HWP_HXS_PCAC_PSTOP 0x00000001
  282. struct fman_port_hwp_regs {
  283. struct {
  284. u32 ssa; /* Soft Sequence Attachment */
  285. u32 lcv; /* Line-up Enable Confirmation Mask */
  286. } pmda[HWP_HXS_COUNT]; /* Parse Memory Direct Access Registers */
  287. u32 reserved080[(0x3f8 - 0x080) / 4]; /* (0x080-0x3f7) */
  288. u32 fmpr_pcac; /* Configuration Access Control */
  289. };
  290. /* QMI dequeue prefetch modes */
  291. enum fman_port_deq_prefetch {
  292. FMAN_PORT_DEQ_NO_PREFETCH, /* No prefetch mode */
  293. FMAN_PORT_DEQ_PART_PREFETCH, /* Partial prefetch mode */
  294. FMAN_PORT_DEQ_FULL_PREFETCH /* Full prefetch mode */
  295. };
  296. /* A structure for defining FM port resources */
  297. struct fman_port_rsrc {
  298. u32 num; /* Committed required resource */
  299. u32 extra; /* Extra (not committed) required resource */
  300. };
  301. enum fman_port_dma_swap {
  302. FMAN_PORT_DMA_NO_SWAP, /* No swap, transfer data as is */
  303. FMAN_PORT_DMA_SWAP_LE,
  304. /* The transferred data should be swapped in PPC Little Endian mode */
  305. FMAN_PORT_DMA_SWAP_BE
  306. /* The transferred data should be swapped in Big Endian mode */
  307. };
  308. /* Default port color */
  309. enum fman_port_color {
  310. FMAN_PORT_COLOR_GREEN, /* Default port color is green */
  311. FMAN_PORT_COLOR_YELLOW, /* Default port color is yellow */
  312. FMAN_PORT_COLOR_RED, /* Default port color is red */
  313. FMAN_PORT_COLOR_OVERRIDE /* Ignore color */
  314. };
  315. /* QMI dequeue from the SP channel - types */
  316. enum fman_port_deq_type {
  317. FMAN_PORT_DEQ_BY_PRI,
  318. /* Priority precedence and Intra-Class scheduling */
  319. FMAN_PORT_DEQ_ACTIVE_FQ,
  320. /* Active FQ precedence and Intra-Class scheduling */
  321. FMAN_PORT_DEQ_ACTIVE_FQ_NO_ICS
  322. /* Active FQ precedence and override Intra-Class scheduling */
  323. };
  324. /* External buffer pools configuration */
  325. struct fman_port_bpools {
  326. u8 count; /* Num of pools to set up */
  327. bool counters_enable; /* Enable allocate counters */
  328. u8 grp_bp_depleted_num;
  329. /* Number of depleted pools - if reached the BMI indicates
  330. * the MAC to send a pause frame
  331. */
  332. struct {
  333. u8 bpid; /* BM pool ID */
  334. u16 size;
  335. /* Pool's size - must be in ascending order */
  336. bool is_backup;
  337. /* If this is a backup pool */
  338. bool grp_bp_depleted;
  339. /* Consider this buffer in multiple pools depletion criteria */
  340. bool single_bp_depleted;
  341. /* Consider this buffer in single pool depletion criteria */
  342. } bpool[FMAN_PORT_MAX_EXT_POOLS_NUM];
  343. };
  344. struct fman_port_cfg {
  345. u32 dflt_fqid;
  346. u32 err_fqid;
  347. u8 deq_sp;
  348. bool deq_high_priority;
  349. enum fman_port_deq_type deq_type;
  350. enum fman_port_deq_prefetch deq_prefetch_option;
  351. u16 deq_byte_cnt;
  352. u8 cheksum_last_bytes_ignore;
  353. u8 rx_cut_end_bytes;
  354. struct fman_buf_pool_depletion buf_pool_depletion;
  355. struct fman_ext_pools ext_buf_pools;
  356. u32 tx_fifo_min_level;
  357. u32 tx_fifo_low_comf_level;
  358. u32 rx_pri_elevation;
  359. u32 rx_fifo_thr;
  360. struct fman_sp_buf_margins buf_margins;
  361. u32 int_buf_start_margin;
  362. struct fman_sp_int_context_data_copy int_context;
  363. u32 discard_mask;
  364. u32 err_mask;
  365. struct fman_buffer_prefix_content buffer_prefix_content;
  366. bool dont_release_buf;
  367. u8 rx_fd_bits;
  368. u32 tx_fifo_deq_pipeline_depth;
  369. bool errata_A006320;
  370. bool excessive_threshold_register;
  371. bool fmbm_tfne_has_features;
  372. enum fman_port_dma_swap dma_swap_data;
  373. enum fman_port_color color;
  374. };
  375. struct fman_port_rx_pools_params {
  376. u8 num_of_pools;
  377. u16 second_largest_buf_size;
  378. u16 largest_buf_size;
  379. };
  380. struct fman_port_dts_params {
  381. void __iomem *base_addr; /* FMan port virtual memory */
  382. enum fman_port_type type; /* Port type */
  383. u16 speed; /* Port speed */
  384. u8 id; /* HW Port Id */
  385. u32 qman_channel_id; /* QMan channel id (non RX only) */
  386. struct fman *fman; /* FMan Handle */
  387. };
  388. struct fman_port {
  389. void *fm;
  390. struct device *dev;
  391. struct fman_rev_info rev_info;
  392. u8 port_id;
  393. enum fman_port_type port_type;
  394. u16 port_speed;
  395. union fman_port_bmi_regs __iomem *bmi_regs;
  396. struct fman_port_qmi_regs __iomem *qmi_regs;
  397. struct fman_port_hwp_regs __iomem *hwp_regs;
  398. struct fman_sp_buffer_offsets buffer_offsets;
  399. u8 internal_buf_offset;
  400. struct fman_ext_pools ext_buf_pools;
  401. u16 max_frame_length;
  402. struct fman_port_rsrc open_dmas;
  403. struct fman_port_rsrc tasks;
  404. struct fman_port_rsrc fifo_bufs;
  405. struct fman_port_rx_pools_params rx_pools_params;
  406. struct fman_port_cfg *cfg;
  407. struct fman_port_dts_params dts_params;
  408. u8 ext_pools_num;
  409. u32 max_port_fifo_size;
  410. u32 max_num_of_ext_pools;
  411. u32 max_num_of_sub_portals;
  412. u32 bm_max_num_of_pools;
  413. };
  414. static int init_bmi_rx(struct fman_port *port)
  415. {
  416. struct fman_port_rx_bmi_regs __iomem *regs = &port->bmi_regs->rx;
  417. struct fman_port_cfg *cfg = port->cfg;
  418. u32 tmp;
  419. /* DMA attributes */
  420. tmp = (u32)cfg->dma_swap_data << BMI_DMA_ATTR_SWP_SHIFT;
  421. /* Enable write optimization */
  422. tmp |= BMI_DMA_ATTR_WRITE_OPTIMIZE;
  423. iowrite32be(tmp, &regs->fmbm_rda);
  424. /* Rx FIFO parameters */
  425. tmp = (cfg->rx_pri_elevation / PORT_BMI_FIFO_UNITS - 1) <<
  426. BMI_RX_FIFO_PRI_ELEVATION_SHIFT;
  427. tmp |= cfg->rx_fifo_thr / PORT_BMI_FIFO_UNITS - 1;
  428. iowrite32be(tmp, &regs->fmbm_rfp);
  429. if (cfg->excessive_threshold_register)
  430. /* always allow access to the extra resources */
  431. iowrite32be(BMI_RX_FIFO_THRESHOLD_ETHE, &regs->fmbm_reth);
  432. /* Frame end data */
  433. tmp = (cfg->cheksum_last_bytes_ignore & BMI_FRAME_END_CS_IGNORE_MASK) <<
  434. BMI_FRAME_END_CS_IGNORE_SHIFT;
  435. tmp |= (cfg->rx_cut_end_bytes & BMI_RX_FRAME_END_CUT_MASK) <<
  436. BMI_RX_FRAME_END_CUT_SHIFT;
  437. if (cfg->errata_A006320)
  438. tmp &= 0xffe0ffff;
  439. iowrite32be(tmp, &regs->fmbm_rfed);
  440. /* Internal context parameters */
  441. tmp = ((cfg->int_context.ext_buf_offset / PORT_IC_OFFSET_UNITS) &
  442. BMI_IC_TO_EXT_MASK) << BMI_IC_TO_EXT_SHIFT;
  443. tmp |= ((cfg->int_context.int_context_offset / PORT_IC_OFFSET_UNITS) &
  444. BMI_IC_FROM_INT_MASK) << BMI_IC_FROM_INT_SHIFT;
  445. tmp |= (cfg->int_context.size / PORT_IC_OFFSET_UNITS) &
  446. BMI_IC_SIZE_MASK;
  447. iowrite32be(tmp, &regs->fmbm_ricp);
  448. /* Internal buffer offset */
  449. tmp = ((cfg->int_buf_start_margin / PORT_IC_OFFSET_UNITS) &
  450. BMI_INT_BUF_MARG_MASK) << BMI_INT_BUF_MARG_SHIFT;
  451. iowrite32be(tmp, &regs->fmbm_rim);
  452. /* External buffer margins */
  453. tmp = (cfg->buf_margins.start_margins & BMI_EXT_BUF_MARG_START_MASK) <<
  454. BMI_EXT_BUF_MARG_START_SHIFT;
  455. tmp |= cfg->buf_margins.end_margins & BMI_EXT_BUF_MARG_END_MASK;
  456. iowrite32be(tmp, &regs->fmbm_rebm);
  457. /* Frame attributes */
  458. tmp = BMI_CMD_RX_MR_DEF;
  459. tmp |= BMI_CMD_ATTR_ORDER;
  460. tmp |= (u32)cfg->color << BMI_CMD_ATTR_COLOR_SHIFT;
  461. /* Synchronization request */
  462. tmp |= BMI_CMD_ATTR_SYNC;
  463. iowrite32be(tmp, &regs->fmbm_rfca);
  464. /* NIA */
  465. tmp = (u32)cfg->rx_fd_bits << BMI_NEXT_ENG_FD_BITS_SHIFT;
  466. tmp |= NIA_ENG_HWP;
  467. iowrite32be(tmp, &regs->fmbm_rfne);
  468. /* Parser Next Engine NIA */
  469. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_ENQ_FRAME, &regs->fmbm_rfpne);
  470. /* Enqueue NIA */
  471. iowrite32be(NIA_ENG_QMI_ENQ | NIA_ORDER_RESTOR, &regs->fmbm_rfene);
  472. /* Default/error queues */
  473. iowrite32be((cfg->dflt_fqid & DFLT_FQ_ID), &regs->fmbm_rfqid);
  474. iowrite32be((cfg->err_fqid & DFLT_FQ_ID), &regs->fmbm_refqid);
  475. /* Discard/error masks */
  476. iowrite32be(cfg->discard_mask, &regs->fmbm_rfsdm);
  477. iowrite32be(cfg->err_mask, &regs->fmbm_rfsem);
  478. return 0;
  479. }
  480. static int init_bmi_tx(struct fman_port *port)
  481. {
  482. struct fman_port_tx_bmi_regs __iomem *regs = &port->bmi_regs->tx;
  483. struct fman_port_cfg *cfg = port->cfg;
  484. u32 tmp;
  485. /* Tx Configuration register */
  486. tmp = 0;
  487. iowrite32be(tmp, &regs->fmbm_tcfg);
  488. /* DMA attributes */
  489. tmp = (u32)cfg->dma_swap_data << BMI_DMA_ATTR_SWP_SHIFT;
  490. iowrite32be(tmp, &regs->fmbm_tda);
  491. /* Tx FIFO parameters */
  492. tmp = (cfg->tx_fifo_min_level / PORT_BMI_FIFO_UNITS) <<
  493. BMI_TX_FIFO_MIN_FILL_SHIFT;
  494. tmp |= ((cfg->tx_fifo_deq_pipeline_depth - 1) &
  495. BMI_FIFO_PIPELINE_DEPTH_MASK) << BMI_FIFO_PIPELINE_DEPTH_SHIFT;
  496. tmp |= (cfg->tx_fifo_low_comf_level / PORT_BMI_FIFO_UNITS) - 1;
  497. iowrite32be(tmp, &regs->fmbm_tfp);
  498. /* Frame end data */
  499. tmp = (cfg->cheksum_last_bytes_ignore & BMI_FRAME_END_CS_IGNORE_MASK) <<
  500. BMI_FRAME_END_CS_IGNORE_SHIFT;
  501. iowrite32be(tmp, &regs->fmbm_tfed);
  502. /* Internal context parameters */
  503. tmp = ((cfg->int_context.ext_buf_offset / PORT_IC_OFFSET_UNITS) &
  504. BMI_IC_TO_EXT_MASK) << BMI_IC_TO_EXT_SHIFT;
  505. tmp |= ((cfg->int_context.int_context_offset / PORT_IC_OFFSET_UNITS) &
  506. BMI_IC_FROM_INT_MASK) << BMI_IC_FROM_INT_SHIFT;
  507. tmp |= (cfg->int_context.size / PORT_IC_OFFSET_UNITS) &
  508. BMI_IC_SIZE_MASK;
  509. iowrite32be(tmp, &regs->fmbm_ticp);
  510. /* Frame attributes */
  511. tmp = BMI_CMD_TX_MR_DEF;
  512. tmp |= BMI_CMD_ATTR_ORDER;
  513. tmp |= (u32)cfg->color << BMI_CMD_ATTR_COLOR_SHIFT;
  514. iowrite32be(tmp, &regs->fmbm_tfca);
  515. /* Dequeue NIA + enqueue NIA */
  516. iowrite32be(NIA_ENG_QMI_DEQ, &regs->fmbm_tfdne);
  517. iowrite32be(NIA_ENG_QMI_ENQ | NIA_ORDER_RESTOR, &regs->fmbm_tfene);
  518. if (cfg->fmbm_tfne_has_features)
  519. iowrite32be(!cfg->dflt_fqid ?
  520. BMI_EBD_EN | NIA_BMI_AC_FETCH_ALL_FRAME :
  521. NIA_BMI_AC_FETCH_ALL_FRAME, &regs->fmbm_tfne);
  522. if (!cfg->dflt_fqid && cfg->dont_release_buf) {
  523. iowrite32be(DFLT_FQ_ID, &regs->fmbm_tcfqid);
  524. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE,
  525. &regs->fmbm_tfene);
  526. if (cfg->fmbm_tfne_has_features)
  527. iowrite32be(ioread32be(&regs->fmbm_tfne) & ~BMI_EBD_EN,
  528. &regs->fmbm_tfne);
  529. }
  530. /* Confirmation/error queues */
  531. if (cfg->dflt_fqid || !cfg->dont_release_buf)
  532. iowrite32be(cfg->dflt_fqid & DFLT_FQ_ID, &regs->fmbm_tcfqid);
  533. iowrite32be((cfg->err_fqid & DFLT_FQ_ID), &regs->fmbm_tefqid);
  534. return 0;
  535. }
  536. static int init_qmi(struct fman_port *port)
  537. {
  538. struct fman_port_qmi_regs __iomem *regs = port->qmi_regs;
  539. struct fman_port_cfg *cfg = port->cfg;
  540. u32 tmp;
  541. /* Rx port configuration */
  542. if (port->port_type == FMAN_PORT_TYPE_RX) {
  543. /* Enqueue NIA */
  544. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_RELEASE, &regs->fmqm_pnen);
  545. return 0;
  546. }
  547. /* Continue with Tx port configuration */
  548. if (port->port_type == FMAN_PORT_TYPE_TX) {
  549. /* Enqueue NIA */
  550. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE,
  551. &regs->fmqm_pnen);
  552. /* Dequeue NIA */
  553. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX, &regs->fmqm_pndn);
  554. }
  555. /* Dequeue Configuration register */
  556. tmp = 0;
  557. if (cfg->deq_high_priority)
  558. tmp |= QMI_DEQ_CFG_PRI;
  559. switch (cfg->deq_type) {
  560. case FMAN_PORT_DEQ_BY_PRI:
  561. tmp |= QMI_DEQ_CFG_TYPE1;
  562. break;
  563. case FMAN_PORT_DEQ_ACTIVE_FQ:
  564. tmp |= QMI_DEQ_CFG_TYPE2;
  565. break;
  566. case FMAN_PORT_DEQ_ACTIVE_FQ_NO_ICS:
  567. tmp |= QMI_DEQ_CFG_TYPE3;
  568. break;
  569. default:
  570. return -EINVAL;
  571. }
  572. switch (cfg->deq_prefetch_option) {
  573. case FMAN_PORT_DEQ_NO_PREFETCH:
  574. break;
  575. case FMAN_PORT_DEQ_PART_PREFETCH:
  576. tmp |= QMI_DEQ_CFG_PREFETCH_PARTIAL;
  577. break;
  578. case FMAN_PORT_DEQ_FULL_PREFETCH:
  579. tmp |= QMI_DEQ_CFG_PREFETCH_FULL;
  580. break;
  581. default:
  582. return -EINVAL;
  583. }
  584. tmp |= (cfg->deq_sp & QMI_DEQ_CFG_SP_MASK) << QMI_DEQ_CFG_SP_SHIFT;
  585. tmp |= cfg->deq_byte_cnt;
  586. iowrite32be(tmp, &regs->fmqm_pndc);
  587. return 0;
  588. }
  589. static void stop_port_hwp(struct fman_port *port)
  590. {
  591. struct fman_port_hwp_regs __iomem *regs = port->hwp_regs;
  592. int cnt = 100;
  593. iowrite32be(HWP_HXS_PCAC_PSTOP, &regs->fmpr_pcac);
  594. while (cnt-- > 0 &&
  595. (ioread32be(&regs->fmpr_pcac) & HWP_HXS_PCAC_PSTAT))
  596. udelay(10);
  597. if (!cnt)
  598. pr_err("Timeout stopping HW Parser\n");
  599. }
  600. static void start_port_hwp(struct fman_port *port)
  601. {
  602. struct fman_port_hwp_regs __iomem *regs = port->hwp_regs;
  603. int cnt = 100;
  604. iowrite32be(0, &regs->fmpr_pcac);
  605. while (cnt-- > 0 &&
  606. !(ioread32be(&regs->fmpr_pcac) & HWP_HXS_PCAC_PSTAT))
  607. udelay(10);
  608. if (!cnt)
  609. pr_err("Timeout starting HW Parser\n");
  610. }
  611. static void init_hwp(struct fman_port *port)
  612. {
  613. struct fman_port_hwp_regs __iomem *regs = port->hwp_regs;
  614. int i;
  615. stop_port_hwp(port);
  616. for (i = 0; i < HWP_HXS_COUNT; i++) {
  617. /* enable HXS error reporting into FD[STATUS] PHE */
  618. iowrite32be(0x00000000, &regs->pmda[i].ssa);
  619. iowrite32be(0xffffffff, &regs->pmda[i].lcv);
  620. }
  621. start_port_hwp(port);
  622. }
  623. static int init(struct fman_port *port)
  624. {
  625. int err;
  626. /* Init BMI registers */
  627. switch (port->port_type) {
  628. case FMAN_PORT_TYPE_RX:
  629. err = init_bmi_rx(port);
  630. if (!err)
  631. init_hwp(port);
  632. break;
  633. case FMAN_PORT_TYPE_TX:
  634. err = init_bmi_tx(port);
  635. break;
  636. default:
  637. return -EINVAL;
  638. }
  639. if (err)
  640. return err;
  641. /* Init QMI registers */
  642. err = init_qmi(port);
  643. if (err)
  644. return err;
  645. return 0;
  646. }
  647. static int set_bpools(const struct fman_port *port,
  648. const struct fman_port_bpools *bp)
  649. {
  650. u32 __iomem *bp_reg, *bp_depl_reg;
  651. u32 tmp;
  652. u8 i, max_bp_num;
  653. bool grp_depl_used = false, rx_port;
  654. switch (port->port_type) {
  655. case FMAN_PORT_TYPE_RX:
  656. max_bp_num = port->ext_pools_num;
  657. rx_port = true;
  658. bp_reg = port->bmi_regs->rx.fmbm_ebmpi;
  659. bp_depl_reg = &port->bmi_regs->rx.fmbm_mpd;
  660. break;
  661. default:
  662. return -EINVAL;
  663. }
  664. if (rx_port) {
  665. /* Check buffers are provided in ascending order */
  666. for (i = 0; (i < (bp->count - 1) &&
  667. (i < FMAN_PORT_MAX_EXT_POOLS_NUM - 1)); i++) {
  668. if (bp->bpool[i].size > bp->bpool[i + 1].size)
  669. return -EINVAL;
  670. }
  671. }
  672. /* Set up external buffers pools */
  673. for (i = 0; i < bp->count; i++) {
  674. tmp = BMI_EXT_BUF_POOL_VALID;
  675. tmp |= ((u32)bp->bpool[i].bpid <<
  676. BMI_EXT_BUF_POOL_ID_SHIFT) & BMI_EXT_BUF_POOL_ID_MASK;
  677. if (rx_port) {
  678. if (bp->counters_enable)
  679. tmp |= BMI_EXT_BUF_POOL_EN_COUNTER;
  680. if (bp->bpool[i].is_backup)
  681. tmp |= BMI_EXT_BUF_POOL_BACKUP;
  682. tmp |= (u32)bp->bpool[i].size;
  683. }
  684. iowrite32be(tmp, &bp_reg[i]);
  685. }
  686. /* Clear unused pools */
  687. for (i = bp->count; i < max_bp_num; i++)
  688. iowrite32be(0, &bp_reg[i]);
  689. /* Pools depletion */
  690. tmp = 0;
  691. for (i = 0; i < FMAN_PORT_MAX_EXT_POOLS_NUM; i++) {
  692. if (bp->bpool[i].grp_bp_depleted) {
  693. grp_depl_used = true;
  694. tmp |= 0x80000000 >> i;
  695. }
  696. if (bp->bpool[i].single_bp_depleted)
  697. tmp |= 0x80 >> i;
  698. }
  699. if (grp_depl_used)
  700. tmp |= ((u32)bp->grp_bp_depleted_num - 1) <<
  701. BMI_POOL_DEP_NUM_OF_POOLS_SHIFT;
  702. iowrite32be(tmp, bp_depl_reg);
  703. return 0;
  704. }
  705. static bool is_init_done(struct fman_port_cfg *cfg)
  706. {
  707. /* Checks if FMan port driver parameters were initialized */
  708. if (!cfg)
  709. return true;
  710. return false;
  711. }
  712. static int verify_size_of_fifo(struct fman_port *port)
  713. {
  714. u32 min_fifo_size_required = 0, opt_fifo_size_for_b2b = 0;
  715. /* TX Ports */
  716. if (port->port_type == FMAN_PORT_TYPE_TX) {
  717. min_fifo_size_required = (u32)
  718. (roundup(port->max_frame_length,
  719. FMAN_BMI_FIFO_UNITS) + (3 * FMAN_BMI_FIFO_UNITS));
  720. min_fifo_size_required +=
  721. port->cfg->tx_fifo_deq_pipeline_depth *
  722. FMAN_BMI_FIFO_UNITS;
  723. opt_fifo_size_for_b2b = min_fifo_size_required;
  724. /* Add some margin for back-to-back capability to improve
  725. * performance, allows the hardware to pipeline new frame dma
  726. * while the previous frame not yet transmitted.
  727. */
  728. if (port->port_speed == 10000)
  729. opt_fifo_size_for_b2b += 3 * FMAN_BMI_FIFO_UNITS;
  730. else
  731. opt_fifo_size_for_b2b += 2 * FMAN_BMI_FIFO_UNITS;
  732. }
  733. /* RX Ports */
  734. else if (port->port_type == FMAN_PORT_TYPE_RX) {
  735. if (port->rev_info.major >= 6)
  736. min_fifo_size_required = (u32)
  737. (roundup(port->max_frame_length,
  738. FMAN_BMI_FIFO_UNITS) +
  739. (5 * FMAN_BMI_FIFO_UNITS));
  740. /* 4 according to spec + 1 for FOF>0 */
  741. else
  742. min_fifo_size_required = (u32)
  743. (roundup(min(port->max_frame_length,
  744. port->rx_pools_params.largest_buf_size),
  745. FMAN_BMI_FIFO_UNITS) +
  746. (7 * FMAN_BMI_FIFO_UNITS));
  747. opt_fifo_size_for_b2b = min_fifo_size_required;
  748. /* Add some margin for back-to-back capability to improve
  749. * performance,allows the hardware to pipeline new frame dma
  750. * while the previous frame not yet transmitted.
  751. */
  752. if (port->port_speed == 10000)
  753. opt_fifo_size_for_b2b += 8 * FMAN_BMI_FIFO_UNITS;
  754. else
  755. opt_fifo_size_for_b2b += 3 * FMAN_BMI_FIFO_UNITS;
  756. }
  757. WARN_ON(min_fifo_size_required <= 0);
  758. WARN_ON(opt_fifo_size_for_b2b < min_fifo_size_required);
  759. /* Verify the size */
  760. if (port->fifo_bufs.num < min_fifo_size_required)
  761. dev_dbg(port->dev, "%s: FIFO size should be enlarged to %d bytes\n",
  762. __func__, min_fifo_size_required);
  763. else if (port->fifo_bufs.num < opt_fifo_size_for_b2b)
  764. dev_dbg(port->dev, "%s: For b2b processing,FIFO may be enlarged to %d bytes\n",
  765. __func__, opt_fifo_size_for_b2b);
  766. return 0;
  767. }
  768. static int set_ext_buffer_pools(struct fman_port *port)
  769. {
  770. struct fman_ext_pools *ext_buf_pools = &port->cfg->ext_buf_pools;
  771. struct fman_buf_pool_depletion *buf_pool_depletion =
  772. &port->cfg->buf_pool_depletion;
  773. u8 ordered_array[FMAN_PORT_MAX_EXT_POOLS_NUM];
  774. u16 sizes_array[BM_MAX_NUM_OF_POOLS];
  775. int i = 0, j = 0, err;
  776. struct fman_port_bpools bpools;
  777. memset(&ordered_array, 0, sizeof(u8) * FMAN_PORT_MAX_EXT_POOLS_NUM);
  778. memset(&sizes_array, 0, sizeof(u16) * BM_MAX_NUM_OF_POOLS);
  779. memcpy(&port->ext_buf_pools, ext_buf_pools,
  780. sizeof(struct fman_ext_pools));
  781. fman_sp_set_buf_pools_in_asc_order_of_buf_sizes(ext_buf_pools,
  782. ordered_array,
  783. sizes_array);
  784. memset(&bpools, 0, sizeof(struct fman_port_bpools));
  785. bpools.count = ext_buf_pools->num_of_pools_used;
  786. bpools.counters_enable = true;
  787. for (i = 0; i < ext_buf_pools->num_of_pools_used; i++) {
  788. bpools.bpool[i].bpid = ordered_array[i];
  789. bpools.bpool[i].size = sizes_array[ordered_array[i]];
  790. }
  791. /* save pools parameters for later use */
  792. port->rx_pools_params.num_of_pools = ext_buf_pools->num_of_pools_used;
  793. port->rx_pools_params.largest_buf_size =
  794. sizes_array[ordered_array[ext_buf_pools->num_of_pools_used - 1]];
  795. port->rx_pools_params.second_largest_buf_size =
  796. sizes_array[ordered_array[ext_buf_pools->num_of_pools_used - 2]];
  797. /* FMBM_RMPD reg. - pool depletion */
  798. if (buf_pool_depletion->pools_grp_mode_enable) {
  799. bpools.grp_bp_depleted_num = buf_pool_depletion->num_of_pools;
  800. for (i = 0; i < port->bm_max_num_of_pools; i++) {
  801. if (buf_pool_depletion->pools_to_consider[i]) {
  802. for (j = 0; j < ext_buf_pools->
  803. num_of_pools_used; j++) {
  804. if (i == ordered_array[j]) {
  805. bpools.bpool[j].
  806. grp_bp_depleted = true;
  807. break;
  808. }
  809. }
  810. }
  811. }
  812. }
  813. if (buf_pool_depletion->single_pool_mode_enable) {
  814. for (i = 0; i < port->bm_max_num_of_pools; i++) {
  815. if (buf_pool_depletion->
  816. pools_to_consider_for_single_mode[i]) {
  817. for (j = 0; j < ext_buf_pools->
  818. num_of_pools_used; j++) {
  819. if (i == ordered_array[j]) {
  820. bpools.bpool[j].
  821. single_bp_depleted = true;
  822. break;
  823. }
  824. }
  825. }
  826. }
  827. }
  828. err = set_bpools(port, &bpools);
  829. if (err != 0) {
  830. dev_err(port->dev, "%s: set_bpools() failed\n", __func__);
  831. return -EINVAL;
  832. }
  833. return 0;
  834. }
  835. static int init_low_level_driver(struct fman_port *port)
  836. {
  837. struct fman_port_cfg *cfg = port->cfg;
  838. u32 tmp_val;
  839. switch (port->port_type) {
  840. case FMAN_PORT_TYPE_RX:
  841. cfg->err_mask = (RX_ERRS_TO_ENQ & ~cfg->discard_mask);
  842. break;
  843. default:
  844. break;
  845. }
  846. tmp_val = (u32)((port->internal_buf_offset % OFFSET_UNITS) ?
  847. (port->internal_buf_offset / OFFSET_UNITS + 1) :
  848. (port->internal_buf_offset / OFFSET_UNITS));
  849. port->internal_buf_offset = (u8)(tmp_val * OFFSET_UNITS);
  850. port->cfg->int_buf_start_margin = port->internal_buf_offset;
  851. if (init(port) != 0) {
  852. dev_err(port->dev, "%s: fman port initialization failed\n",
  853. __func__);
  854. return -ENODEV;
  855. }
  856. /* The code bellow is a trick so the FM will not release the buffer
  857. * to BM nor will try to enqueue the frame to QM
  858. */
  859. if (port->port_type == FMAN_PORT_TYPE_TX) {
  860. if (!cfg->dflt_fqid && cfg->dont_release_buf) {
  861. /* override fmbm_tcfqid 0 with a false non-0 value.
  862. * This will force FM to act according to tfene.
  863. * Otherwise, if fmbm_tcfqid is 0 the FM will release
  864. * buffers to BM regardless of fmbm_tfene
  865. */
  866. iowrite32be(0xFFFFFF, &port->bmi_regs->tx.fmbm_tcfqid);
  867. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE,
  868. &port->bmi_regs->tx.fmbm_tfene);
  869. }
  870. }
  871. return 0;
  872. }
  873. static int fill_soc_specific_params(struct fman_port *port)
  874. {
  875. u32 bmi_max_fifo_size;
  876. bmi_max_fifo_size = fman_get_bmi_max_fifo_size(port->fm);
  877. port->max_port_fifo_size = MAX_PORT_FIFO_SIZE(bmi_max_fifo_size);
  878. port->bm_max_num_of_pools = 64;
  879. /* P4080 - Major 2
  880. * P2041/P3041/P5020/P5040 - Major 3
  881. * Tx/Bx - Major 6
  882. */
  883. switch (port->rev_info.major) {
  884. case 2:
  885. case 3:
  886. port->max_num_of_ext_pools = 4;
  887. port->max_num_of_sub_portals = 12;
  888. break;
  889. case 6:
  890. port->max_num_of_ext_pools = 8;
  891. port->max_num_of_sub_portals = 16;
  892. break;
  893. default:
  894. dev_err(port->dev, "%s: Unsupported FMan version\n", __func__);
  895. return -EINVAL;
  896. }
  897. return 0;
  898. }
  899. static int get_dflt_fifo_deq_pipeline_depth(u8 major, enum fman_port_type type,
  900. u16 speed)
  901. {
  902. switch (type) {
  903. case FMAN_PORT_TYPE_RX:
  904. case FMAN_PORT_TYPE_TX:
  905. switch (speed) {
  906. case 10000:
  907. return 4;
  908. case 1000:
  909. if (major >= 6)
  910. return 2;
  911. else
  912. return 1;
  913. default:
  914. return 0;
  915. }
  916. default:
  917. return 0;
  918. }
  919. }
  920. static int get_dflt_num_of_tasks(u8 major, enum fman_port_type type,
  921. u16 speed)
  922. {
  923. switch (type) {
  924. case FMAN_PORT_TYPE_RX:
  925. case FMAN_PORT_TYPE_TX:
  926. switch (speed) {
  927. case 10000:
  928. return 16;
  929. case 1000:
  930. if (major >= 6)
  931. return 4;
  932. else
  933. return 3;
  934. default:
  935. return 0;
  936. }
  937. default:
  938. return 0;
  939. }
  940. }
  941. static int get_dflt_extra_num_of_tasks(u8 major, enum fman_port_type type,
  942. u16 speed)
  943. {
  944. switch (type) {
  945. case FMAN_PORT_TYPE_RX:
  946. /* FMan V3 */
  947. if (major >= 6)
  948. return 0;
  949. /* FMan V2 */
  950. if (speed == 10000)
  951. return 8;
  952. else
  953. return 2;
  954. case FMAN_PORT_TYPE_TX:
  955. default:
  956. return 0;
  957. }
  958. }
  959. static int get_dflt_num_of_open_dmas(u8 major, enum fman_port_type type,
  960. u16 speed)
  961. {
  962. int val;
  963. if (major >= 6) {
  964. switch (type) {
  965. case FMAN_PORT_TYPE_TX:
  966. if (speed == 10000)
  967. val = 12;
  968. else
  969. val = 3;
  970. break;
  971. case FMAN_PORT_TYPE_RX:
  972. if (speed == 10000)
  973. val = 8;
  974. else
  975. val = 2;
  976. break;
  977. default:
  978. return 0;
  979. }
  980. } else {
  981. switch (type) {
  982. case FMAN_PORT_TYPE_TX:
  983. case FMAN_PORT_TYPE_RX:
  984. if (speed == 10000)
  985. val = 8;
  986. else
  987. val = 1;
  988. break;
  989. default:
  990. val = 0;
  991. }
  992. }
  993. return val;
  994. }
  995. static int get_dflt_extra_num_of_open_dmas(u8 major, enum fman_port_type type,
  996. u16 speed)
  997. {
  998. /* FMan V3 */
  999. if (major >= 6)
  1000. return 0;
  1001. /* FMan V2 */
  1002. switch (type) {
  1003. case FMAN_PORT_TYPE_RX:
  1004. case FMAN_PORT_TYPE_TX:
  1005. if (speed == 10000)
  1006. return 8;
  1007. else
  1008. return 1;
  1009. default:
  1010. return 0;
  1011. }
  1012. }
  1013. static int get_dflt_num_of_fifo_bufs(u8 major, enum fman_port_type type,
  1014. u16 speed)
  1015. {
  1016. int val;
  1017. if (major >= 6) {
  1018. switch (type) {
  1019. case FMAN_PORT_TYPE_TX:
  1020. if (speed == 10000)
  1021. val = 64;
  1022. else
  1023. val = 50;
  1024. break;
  1025. case FMAN_PORT_TYPE_RX:
  1026. if (speed == 10000)
  1027. val = 96;
  1028. else
  1029. val = 50;
  1030. break;
  1031. default:
  1032. val = 0;
  1033. }
  1034. } else {
  1035. switch (type) {
  1036. case FMAN_PORT_TYPE_TX:
  1037. if (speed == 10000)
  1038. val = 48;
  1039. else
  1040. val = 44;
  1041. break;
  1042. case FMAN_PORT_TYPE_RX:
  1043. if (speed == 10000)
  1044. val = 48;
  1045. else
  1046. val = 45;
  1047. break;
  1048. default:
  1049. val = 0;
  1050. }
  1051. }
  1052. return val;
  1053. }
  1054. static void set_dflt_cfg(struct fman_port *port,
  1055. struct fman_port_params *port_params)
  1056. {
  1057. struct fman_port_cfg *cfg = port->cfg;
  1058. cfg->dma_swap_data = FMAN_PORT_DMA_NO_SWAP;
  1059. cfg->color = FMAN_PORT_COLOR_GREEN;
  1060. cfg->rx_cut_end_bytes = DFLT_PORT_CUT_BYTES_FROM_END;
  1061. cfg->rx_pri_elevation = BMI_PRIORITY_ELEVATION_LEVEL;
  1062. cfg->rx_fifo_thr = BMI_FIFO_THRESHOLD;
  1063. cfg->tx_fifo_low_comf_level = (5 * 1024);
  1064. cfg->deq_type = FMAN_PORT_DEQ_BY_PRI;
  1065. cfg->deq_prefetch_option = FMAN_PORT_DEQ_FULL_PREFETCH;
  1066. cfg->tx_fifo_deq_pipeline_depth =
  1067. BMI_DEQUEUE_PIPELINE_DEPTH(port->port_type, port->port_speed);
  1068. cfg->deq_byte_cnt = QMI_BYTE_COUNT_LEVEL_CONTROL(port->port_type);
  1069. cfg->rx_pri_elevation =
  1070. DFLT_PORT_RX_FIFO_PRI_ELEVATION_LEV(port->max_port_fifo_size);
  1071. port->cfg->rx_fifo_thr =
  1072. DFLT_PORT_RX_FIFO_THRESHOLD(port->rev_info.major,
  1073. port->max_port_fifo_size);
  1074. if ((port->rev_info.major == 6) &&
  1075. ((port->rev_info.minor == 0) || (port->rev_info.minor == 3)))
  1076. cfg->errata_A006320 = true;
  1077. /* Excessive Threshold register - exists for pre-FMv3 chips only */
  1078. if (port->rev_info.major < 6)
  1079. cfg->excessive_threshold_register = true;
  1080. else
  1081. cfg->fmbm_tfne_has_features = true;
  1082. cfg->buffer_prefix_content.data_align =
  1083. DFLT_PORT_BUFFER_PREFIX_CONTEXT_DATA_ALIGN;
  1084. }
  1085. static void set_rx_dflt_cfg(struct fman_port *port,
  1086. struct fman_port_params *port_params)
  1087. {
  1088. port->cfg->discard_mask = DFLT_PORT_ERRORS_TO_DISCARD;
  1089. memcpy(&port->cfg->ext_buf_pools,
  1090. &port_params->specific_params.rx_params.ext_buf_pools,
  1091. sizeof(struct fman_ext_pools));
  1092. port->cfg->err_fqid =
  1093. port_params->specific_params.rx_params.err_fqid;
  1094. port->cfg->dflt_fqid =
  1095. port_params->specific_params.rx_params.dflt_fqid;
  1096. }
  1097. static void set_tx_dflt_cfg(struct fman_port *port,
  1098. struct fman_port_params *port_params,
  1099. struct fman_port_dts_params *dts_params)
  1100. {
  1101. port->cfg->tx_fifo_deq_pipeline_depth =
  1102. get_dflt_fifo_deq_pipeline_depth(port->rev_info.major,
  1103. port->port_type,
  1104. port->port_speed);
  1105. port->cfg->err_fqid =
  1106. port_params->specific_params.non_rx_params.err_fqid;
  1107. port->cfg->deq_sp =
  1108. (u8)(dts_params->qman_channel_id & QMI_DEQ_CFG_SUBPORTAL_MASK);
  1109. port->cfg->dflt_fqid =
  1110. port_params->specific_params.non_rx_params.dflt_fqid;
  1111. port->cfg->deq_high_priority = true;
  1112. }
  1113. /**
  1114. * fman_port_config
  1115. * @port: Pointer to the port structure
  1116. * @params: Pointer to data structure of parameters
  1117. *
  1118. * Creates a descriptor for the FM PORT module.
  1119. * The routine returns a pointer to the FM PORT object.
  1120. * This descriptor must be passed as first parameter to all other FM PORT
  1121. * function calls.
  1122. * No actual initialization or configuration of FM hardware is done by this
  1123. * routine.
  1124. *
  1125. * Return: 0 on success; Error code otherwise.
  1126. */
  1127. int fman_port_config(struct fman_port *port, struct fman_port_params *params)
  1128. {
  1129. void __iomem *base_addr = port->dts_params.base_addr;
  1130. int err;
  1131. /* Allocate the FM driver's parameters structure */
  1132. port->cfg = kzalloc(sizeof(*port->cfg), GFP_KERNEL);
  1133. if (!port->cfg)
  1134. return -EINVAL;
  1135. /* Initialize FM port parameters which will be kept by the driver */
  1136. port->port_type = port->dts_params.type;
  1137. port->port_speed = port->dts_params.speed;
  1138. port->port_id = port->dts_params.id;
  1139. port->fm = port->dts_params.fman;
  1140. port->ext_pools_num = (u8)8;
  1141. /* get FM revision */
  1142. fman_get_revision(port->fm, &port->rev_info);
  1143. err = fill_soc_specific_params(port);
  1144. if (err)
  1145. goto err_port_cfg;
  1146. switch (port->port_type) {
  1147. case FMAN_PORT_TYPE_RX:
  1148. set_rx_dflt_cfg(port, params);
  1149. case FMAN_PORT_TYPE_TX:
  1150. set_tx_dflt_cfg(port, params, &port->dts_params);
  1151. default:
  1152. set_dflt_cfg(port, params);
  1153. }
  1154. /* Continue with other parameters */
  1155. /* set memory map pointers */
  1156. port->bmi_regs = base_addr + BMI_PORT_REGS_OFFSET;
  1157. port->qmi_regs = base_addr + QMI_PORT_REGS_OFFSET;
  1158. port->hwp_regs = base_addr + HWP_PORT_REGS_OFFSET;
  1159. port->max_frame_length = DFLT_PORT_MAX_FRAME_LENGTH;
  1160. /* resource distribution. */
  1161. port->fifo_bufs.num =
  1162. get_dflt_num_of_fifo_bufs(port->rev_info.major, port->port_type,
  1163. port->port_speed) * FMAN_BMI_FIFO_UNITS;
  1164. port->fifo_bufs.extra =
  1165. DFLT_PORT_EXTRA_NUM_OF_FIFO_BUFS * FMAN_BMI_FIFO_UNITS;
  1166. port->open_dmas.num =
  1167. get_dflt_num_of_open_dmas(port->rev_info.major,
  1168. port->port_type, port->port_speed);
  1169. port->open_dmas.extra =
  1170. get_dflt_extra_num_of_open_dmas(port->rev_info.major,
  1171. port->port_type, port->port_speed);
  1172. port->tasks.num =
  1173. get_dflt_num_of_tasks(port->rev_info.major,
  1174. port->port_type, port->port_speed);
  1175. port->tasks.extra =
  1176. get_dflt_extra_num_of_tasks(port->rev_info.major,
  1177. port->port_type, port->port_speed);
  1178. /* FM_HEAVY_TRAFFIC_SEQUENCER_HANG_ERRATA_FMAN_A006981 errata
  1179. * workaround
  1180. */
  1181. if ((port->rev_info.major == 6) && (port->rev_info.minor == 0) &&
  1182. (((port->port_type == FMAN_PORT_TYPE_TX) &&
  1183. (port->port_speed == 1000)))) {
  1184. port->open_dmas.num = 16;
  1185. port->open_dmas.extra = 0;
  1186. }
  1187. if (port->rev_info.major >= 6 &&
  1188. port->port_type == FMAN_PORT_TYPE_TX &&
  1189. port->port_speed == 1000) {
  1190. /* FM_WRONG_RESET_VALUES_ERRATA_FMAN_A005127 Errata
  1191. * workaround
  1192. */
  1193. if (port->rev_info.major >= 6) {
  1194. u32 reg;
  1195. reg = 0x00001013;
  1196. iowrite32be(reg, &port->bmi_regs->tx.fmbm_tfp);
  1197. }
  1198. }
  1199. return 0;
  1200. err_port_cfg:
  1201. kfree(port->cfg);
  1202. return -EINVAL;
  1203. }
  1204. EXPORT_SYMBOL(fman_port_config);
  1205. /**
  1206. * fman_port_init
  1207. * port: A pointer to a FM Port module.
  1208. * Initializes the FM PORT module by defining the software structure and
  1209. * configuring the hardware registers.
  1210. *
  1211. * Return: 0 on success; Error code otherwise.
  1212. */
  1213. int fman_port_init(struct fman_port *port)
  1214. {
  1215. struct fman_port_cfg *cfg;
  1216. int err;
  1217. struct fman_port_init_params params;
  1218. if (is_init_done(port->cfg))
  1219. return -EINVAL;
  1220. err = fman_sp_build_buffer_struct(&port->cfg->int_context,
  1221. &port->cfg->buffer_prefix_content,
  1222. &port->cfg->buf_margins,
  1223. &port->buffer_offsets,
  1224. &port->internal_buf_offset);
  1225. if (err)
  1226. return err;
  1227. cfg = port->cfg;
  1228. if (port->port_type == FMAN_PORT_TYPE_RX) {
  1229. /* Call the external Buffer routine which also checks fifo
  1230. * size and updates it if necessary
  1231. */
  1232. /* define external buffer pools and pool depletion */
  1233. err = set_ext_buffer_pools(port);
  1234. if (err)
  1235. return err;
  1236. /* check if the largest external buffer pool is large enough */
  1237. if (cfg->buf_margins.start_margins + MIN_EXT_BUF_SIZE +
  1238. cfg->buf_margins.end_margins >
  1239. port->rx_pools_params.largest_buf_size) {
  1240. dev_err(port->dev, "%s: buf_margins.start_margins (%d) + minimum buf size (64) + buf_margins.end_margins (%d) is larger than maximum external buffer size (%d)\n",
  1241. __func__, cfg->buf_margins.start_margins,
  1242. cfg->buf_margins.end_margins,
  1243. port->rx_pools_params.largest_buf_size);
  1244. return -EINVAL;
  1245. }
  1246. }
  1247. /* Call FM module routine for communicating parameters */
  1248. memset(&params, 0, sizeof(params));
  1249. params.port_id = port->port_id;
  1250. params.port_type = port->port_type;
  1251. params.port_speed = port->port_speed;
  1252. params.num_of_tasks = (u8)port->tasks.num;
  1253. params.num_of_extra_tasks = (u8)port->tasks.extra;
  1254. params.num_of_open_dmas = (u8)port->open_dmas.num;
  1255. params.num_of_extra_open_dmas = (u8)port->open_dmas.extra;
  1256. if (port->fifo_bufs.num) {
  1257. err = verify_size_of_fifo(port);
  1258. if (err)
  1259. return err;
  1260. }
  1261. params.size_of_fifo = port->fifo_bufs.num;
  1262. params.extra_size_of_fifo = port->fifo_bufs.extra;
  1263. params.deq_pipeline_depth = port->cfg->tx_fifo_deq_pipeline_depth;
  1264. params.max_frame_length = port->max_frame_length;
  1265. err = fman_set_port_params(port->fm, &params);
  1266. if (err)
  1267. return err;
  1268. err = init_low_level_driver(port);
  1269. if (err)
  1270. return err;
  1271. kfree(port->cfg);
  1272. port->cfg = NULL;
  1273. return 0;
  1274. }
  1275. EXPORT_SYMBOL(fman_port_init);
  1276. /**
  1277. * fman_port_cfg_buf_prefix_content
  1278. * @port A pointer to a FM Port module.
  1279. * @buffer_prefix_content A structure of parameters describing
  1280. * the structure of the buffer.
  1281. * Out parameter:
  1282. * Start margin - offset of data from
  1283. * start of external buffer.
  1284. * Defines the structure, size and content of the application buffer.
  1285. * The prefix, in Tx ports, if 'pass_prs_result', the application should set
  1286. * a value to their offsets in the prefix of the FM will save the first
  1287. * 'priv_data_size', than, depending on 'pass_prs_result' and
  1288. * 'pass_time_stamp', copy parse result and timeStamp, and the packet itself
  1289. * (in this order), to the application buffer, and to offset.
  1290. * Calling this routine changes the buffer margins definitions in the internal
  1291. * driver data base from its default configuration:
  1292. * Data size: [DEFAULT_PORT_BUFFER_PREFIX_CONTENT_PRIV_DATA_SIZE]
  1293. * Pass Parser result: [DEFAULT_PORT_BUFFER_PREFIX_CONTENT_PASS_PRS_RESULT].
  1294. * Pass timestamp: [DEFAULT_PORT_BUFFER_PREFIX_CONTENT_PASS_TIME_STAMP].
  1295. * May be used for all ports
  1296. *
  1297. * Allowed only following fman_port_config() and before fman_port_init().
  1298. *
  1299. * Return: 0 on success; Error code otherwise.
  1300. */
  1301. int fman_port_cfg_buf_prefix_content(struct fman_port *port,
  1302. struct fman_buffer_prefix_content *
  1303. buffer_prefix_content)
  1304. {
  1305. if (is_init_done(port->cfg))
  1306. return -EINVAL;
  1307. memcpy(&port->cfg->buffer_prefix_content,
  1308. buffer_prefix_content,
  1309. sizeof(struct fman_buffer_prefix_content));
  1310. /* if data_align was not initialized by user,
  1311. * we return to driver's default
  1312. */
  1313. if (!port->cfg->buffer_prefix_content.data_align)
  1314. port->cfg->buffer_prefix_content.data_align =
  1315. DFLT_PORT_BUFFER_PREFIX_CONTEXT_DATA_ALIGN;
  1316. return 0;
  1317. }
  1318. EXPORT_SYMBOL(fman_port_cfg_buf_prefix_content);
  1319. /**
  1320. * fman_port_disable
  1321. * port: A pointer to a FM Port module.
  1322. *
  1323. * Gracefully disable an FM port. The port will not start new tasks after all
  1324. * tasks associated with the port are terminated.
  1325. *
  1326. * This is a blocking routine, it returns after port is gracefully stopped,
  1327. * i.e. the port will not except new frames, but it will finish all frames
  1328. * or tasks which were already began.
  1329. * Allowed only following fman_port_init().
  1330. *
  1331. * Return: 0 on success; Error code otherwise.
  1332. */
  1333. int fman_port_disable(struct fman_port *port)
  1334. {
  1335. u32 __iomem *bmi_cfg_reg, *bmi_status_reg;
  1336. u32 tmp;
  1337. bool rx_port, failure = false;
  1338. int count;
  1339. if (!is_init_done(port->cfg))
  1340. return -EINVAL;
  1341. switch (port->port_type) {
  1342. case FMAN_PORT_TYPE_RX:
  1343. bmi_cfg_reg = &port->bmi_regs->rx.fmbm_rcfg;
  1344. bmi_status_reg = &port->bmi_regs->rx.fmbm_rst;
  1345. rx_port = true;
  1346. break;
  1347. case FMAN_PORT_TYPE_TX:
  1348. bmi_cfg_reg = &port->bmi_regs->tx.fmbm_tcfg;
  1349. bmi_status_reg = &port->bmi_regs->tx.fmbm_tst;
  1350. rx_port = false;
  1351. break;
  1352. default:
  1353. return -EINVAL;
  1354. }
  1355. /* Disable QMI */
  1356. if (!rx_port) {
  1357. tmp = ioread32be(&port->qmi_regs->fmqm_pnc) & ~QMI_PORT_CFG_EN;
  1358. iowrite32be(tmp, &port->qmi_regs->fmqm_pnc);
  1359. /* Wait for QMI to finish FD handling */
  1360. count = 100;
  1361. do {
  1362. udelay(10);
  1363. tmp = ioread32be(&port->qmi_regs->fmqm_pns);
  1364. } while ((tmp & QMI_PORT_STATUS_DEQ_FD_BSY) && --count);
  1365. if (count == 0) {
  1366. /* Timeout */
  1367. failure = true;
  1368. }
  1369. }
  1370. /* Disable BMI */
  1371. tmp = ioread32be(bmi_cfg_reg) & ~BMI_PORT_CFG_EN;
  1372. iowrite32be(tmp, bmi_cfg_reg);
  1373. /* Wait for graceful stop end */
  1374. count = 500;
  1375. do {
  1376. udelay(10);
  1377. tmp = ioread32be(bmi_status_reg);
  1378. } while ((tmp & BMI_PORT_STATUS_BSY) && --count);
  1379. if (count == 0) {
  1380. /* Timeout */
  1381. failure = true;
  1382. }
  1383. if (failure)
  1384. dev_dbg(port->dev, "%s: FMan Port[%d]: BMI or QMI is Busy. Port forced down\n",
  1385. __func__, port->port_id);
  1386. return 0;
  1387. }
  1388. EXPORT_SYMBOL(fman_port_disable);
  1389. /**
  1390. * fman_port_enable
  1391. * port: A pointer to a FM Port module.
  1392. *
  1393. * A runtime routine provided to allow disable/enable of port.
  1394. *
  1395. * Allowed only following fman_port_init().
  1396. *
  1397. * Return: 0 on success; Error code otherwise.
  1398. */
  1399. int fman_port_enable(struct fman_port *port)
  1400. {
  1401. u32 __iomem *bmi_cfg_reg;
  1402. u32 tmp;
  1403. bool rx_port;
  1404. if (!is_init_done(port->cfg))
  1405. return -EINVAL;
  1406. switch (port->port_type) {
  1407. case FMAN_PORT_TYPE_RX:
  1408. bmi_cfg_reg = &port->bmi_regs->rx.fmbm_rcfg;
  1409. rx_port = true;
  1410. break;
  1411. case FMAN_PORT_TYPE_TX:
  1412. bmi_cfg_reg = &port->bmi_regs->tx.fmbm_tcfg;
  1413. rx_port = false;
  1414. break;
  1415. default:
  1416. return -EINVAL;
  1417. }
  1418. /* Enable QMI */
  1419. if (!rx_port) {
  1420. tmp = ioread32be(&port->qmi_regs->fmqm_pnc) | QMI_PORT_CFG_EN;
  1421. iowrite32be(tmp, &port->qmi_regs->fmqm_pnc);
  1422. }
  1423. /* Enable BMI */
  1424. tmp = ioread32be(bmi_cfg_reg) | BMI_PORT_CFG_EN;
  1425. iowrite32be(tmp, bmi_cfg_reg);
  1426. return 0;
  1427. }
  1428. EXPORT_SYMBOL(fman_port_enable);
  1429. /**
  1430. * fman_port_bind
  1431. * dev: FMan Port OF device pointer
  1432. *
  1433. * Bind to a specific FMan Port.
  1434. *
  1435. * Allowed only after the port was created.
  1436. *
  1437. * Return: A pointer to the FMan port device.
  1438. */
  1439. struct fman_port *fman_port_bind(struct device *dev)
  1440. {
  1441. return (struct fman_port *)(dev_get_drvdata(get_device(dev)));
  1442. }
  1443. EXPORT_SYMBOL(fman_port_bind);
  1444. /**
  1445. * fman_port_get_qman_channel_id
  1446. * port: Pointer to the FMan port devuce
  1447. *
  1448. * Get the QMan channel ID for the specific port
  1449. *
  1450. * Return: QMan channel ID
  1451. */
  1452. u32 fman_port_get_qman_channel_id(struct fman_port *port)
  1453. {
  1454. return port->dts_params.qman_channel_id;
  1455. }
  1456. EXPORT_SYMBOL(fman_port_get_qman_channel_id);
  1457. static int fman_port_probe(struct platform_device *of_dev)
  1458. {
  1459. struct fman_port *port;
  1460. struct fman *fman;
  1461. struct device_node *fm_node, *port_node;
  1462. struct resource res;
  1463. struct resource *dev_res;
  1464. u32 val;
  1465. int err = 0, lenp;
  1466. enum fman_port_type port_type;
  1467. u16 port_speed;
  1468. u8 port_id;
  1469. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1470. if (!port)
  1471. return -ENOMEM;
  1472. port->dev = &of_dev->dev;
  1473. port_node = of_node_get(of_dev->dev.of_node);
  1474. /* Get the FM node */
  1475. fm_node = of_get_parent(port_node);
  1476. if (!fm_node) {
  1477. dev_err(port->dev, "%s: of_get_parent() failed\n", __func__);
  1478. err = -ENODEV;
  1479. goto return_err;
  1480. }
  1481. fman = dev_get_drvdata(&of_find_device_by_node(fm_node)->dev);
  1482. of_node_put(fm_node);
  1483. if (!fman) {
  1484. err = -EINVAL;
  1485. goto return_err;
  1486. }
  1487. err = of_property_read_u32(port_node, "cell-index", &val);
  1488. if (err) {
  1489. dev_err(port->dev, "%s: reading cell-index for %s failed\n",
  1490. __func__, port_node->full_name);
  1491. err = -EINVAL;
  1492. goto return_err;
  1493. }
  1494. port_id = (u8)val;
  1495. port->dts_params.id = port_id;
  1496. if (of_device_is_compatible(port_node, "fsl,fman-v3-port-tx")) {
  1497. port_type = FMAN_PORT_TYPE_TX;
  1498. port_speed = 1000;
  1499. if (of_find_property(port_node, "fsl,fman-10g-port", &lenp))
  1500. port_speed = 10000;
  1501. } else if (of_device_is_compatible(port_node, "fsl,fman-v2-port-tx")) {
  1502. if (port_id >= TX_10G_PORT_BASE)
  1503. port_speed = 10000;
  1504. else
  1505. port_speed = 1000;
  1506. port_type = FMAN_PORT_TYPE_TX;
  1507. } else if (of_device_is_compatible(port_node, "fsl,fman-v3-port-rx")) {
  1508. port_type = FMAN_PORT_TYPE_RX;
  1509. port_speed = 1000;
  1510. if (of_find_property(port_node, "fsl,fman-10g-port", &lenp))
  1511. port_speed = 10000;
  1512. } else if (of_device_is_compatible(port_node, "fsl,fman-v2-port-rx")) {
  1513. if (port_id >= RX_10G_PORT_BASE)
  1514. port_speed = 10000;
  1515. else
  1516. port_speed = 1000;
  1517. port_type = FMAN_PORT_TYPE_RX;
  1518. } else {
  1519. dev_err(port->dev, "%s: Illegal port type\n", __func__);
  1520. err = -EINVAL;
  1521. goto return_err;
  1522. }
  1523. port->dts_params.type = port_type;
  1524. port->dts_params.speed = port_speed;
  1525. if (port_type == FMAN_PORT_TYPE_TX) {
  1526. u32 qman_channel_id;
  1527. qman_channel_id = fman_get_qman_channel_id(fman, port_id);
  1528. if (qman_channel_id == 0) {
  1529. dev_err(port->dev, "%s: incorrect qman-channel-id\n",
  1530. __func__);
  1531. err = -EINVAL;
  1532. goto return_err;
  1533. }
  1534. port->dts_params.qman_channel_id = qman_channel_id;
  1535. }
  1536. err = of_address_to_resource(port_node, 0, &res);
  1537. if (err < 0) {
  1538. dev_err(port->dev, "%s: of_address_to_resource() failed\n",
  1539. __func__);
  1540. err = -ENOMEM;
  1541. goto return_err;
  1542. }
  1543. port->dts_params.fman = fman;
  1544. of_node_put(port_node);
  1545. dev_res = __devm_request_region(port->dev, &res, res.start,
  1546. resource_size(&res), "fman-port");
  1547. if (!dev_res) {
  1548. dev_err(port->dev, "%s: __devm_request_region() failed\n",
  1549. __func__);
  1550. err = -EINVAL;
  1551. goto free_port;
  1552. }
  1553. port->dts_params.base_addr = devm_ioremap(port->dev, res.start,
  1554. resource_size(&res));
  1555. if (!port->dts_params.base_addr)
  1556. dev_err(port->dev, "%s: devm_ioremap() failed\n", __func__);
  1557. dev_set_drvdata(&of_dev->dev, port);
  1558. return 0;
  1559. return_err:
  1560. of_node_put(port_node);
  1561. free_port:
  1562. kfree(port);
  1563. return err;
  1564. }
  1565. static const struct of_device_id fman_port_match[] = {
  1566. {.compatible = "fsl,fman-v3-port-rx"},
  1567. {.compatible = "fsl,fman-v2-port-rx"},
  1568. {.compatible = "fsl,fman-v3-port-tx"},
  1569. {.compatible = "fsl,fman-v2-port-tx"},
  1570. {}
  1571. };
  1572. MODULE_DEVICE_TABLE(of, fman_port_match);
  1573. static struct platform_driver fman_port_driver = {
  1574. .driver = {
  1575. .name = "fsl-fman-port",
  1576. .of_match_table = fman_port_match,
  1577. },
  1578. .probe = fman_port_probe,
  1579. };
  1580. static int __init fman_port_load(void)
  1581. {
  1582. int err;
  1583. pr_debug("FSL DPAA FMan driver\n");
  1584. err = platform_driver_register(&fman_port_driver);
  1585. if (err < 0)
  1586. pr_err("Error, platform_driver_register() = %d\n", err);
  1587. return err;
  1588. }
  1589. module_init(fman_port_load);
  1590. static void __exit fman_port_unload(void)
  1591. {
  1592. platform_driver_unregister(&fman_port_driver);
  1593. }
  1594. module_exit(fman_port_unload);
  1595. MODULE_LICENSE("Dual BSD/GPL");
  1596. MODULE_DESCRIPTION("Freescale DPAA Frame Manager Port driver");