ixp4xx_hss.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel IXP4xx HSS (synchronous serial port) driver for Linux
  4. *
  5. * Copyright (C) 2007-2008 Krzysztof Hałasa <khc@pm.waw.pl>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/bitops.h>
  10. #include <linux/cdev.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/fs.h>
  14. #include <linux/hdlc.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/poll.h>
  19. #include <linux/slab.h>
  20. #include <linux/soc/ixp4xx/npe.h>
  21. #include <linux/soc/ixp4xx/qmgr.h>
  22. #define DEBUG_DESC 0
  23. #define DEBUG_RX 0
  24. #define DEBUG_TX 0
  25. #define DEBUG_PKT_BYTES 0
  26. #define DEBUG_CLOSE 0
  27. #define DRV_NAME "ixp4xx_hss"
  28. #define PKT_EXTRA_FLAGS 0 /* orig 1 */
  29. #define PKT_NUM_PIPES 1 /* 1, 2 or 4 */
  30. #define PKT_PIPE_FIFO_SIZEW 4 /* total 4 dwords per HSS */
  31. #define RX_DESCS 16 /* also length of all RX queues */
  32. #define TX_DESCS 16 /* also length of all TX queues */
  33. #define POOL_ALLOC_SIZE (sizeof(struct desc) * (RX_DESCS + TX_DESCS))
  34. #define RX_SIZE (HDLC_MAX_MRU + 4) /* NPE needs more space */
  35. #define MAX_CLOSE_WAIT 1000 /* microseconds */
  36. #define HSS_COUNT 2
  37. #define FRAME_SIZE 256 /* doesn't matter at this point */
  38. #define FRAME_OFFSET 0
  39. #define MAX_CHANNELS (FRAME_SIZE / 8)
  40. #define NAPI_WEIGHT 16
  41. /* Queue IDs */
  42. #define HSS0_CHL_RXTRIG_QUEUE 12 /* orig size = 32 dwords */
  43. #define HSS0_PKT_RX_QUEUE 13 /* orig size = 32 dwords */
  44. #define HSS0_PKT_TX0_QUEUE 14 /* orig size = 16 dwords */
  45. #define HSS0_PKT_TX1_QUEUE 15
  46. #define HSS0_PKT_TX2_QUEUE 16
  47. #define HSS0_PKT_TX3_QUEUE 17
  48. #define HSS0_PKT_RXFREE0_QUEUE 18 /* orig size = 16 dwords */
  49. #define HSS0_PKT_RXFREE1_QUEUE 19
  50. #define HSS0_PKT_RXFREE2_QUEUE 20
  51. #define HSS0_PKT_RXFREE3_QUEUE 21
  52. #define HSS0_PKT_TXDONE_QUEUE 22 /* orig size = 64 dwords */
  53. #define HSS1_CHL_RXTRIG_QUEUE 10
  54. #define HSS1_PKT_RX_QUEUE 0
  55. #define HSS1_PKT_TX0_QUEUE 5
  56. #define HSS1_PKT_TX1_QUEUE 6
  57. #define HSS1_PKT_TX2_QUEUE 7
  58. #define HSS1_PKT_TX3_QUEUE 8
  59. #define HSS1_PKT_RXFREE0_QUEUE 1
  60. #define HSS1_PKT_RXFREE1_QUEUE 2
  61. #define HSS1_PKT_RXFREE2_QUEUE 3
  62. #define HSS1_PKT_RXFREE3_QUEUE 4
  63. #define HSS1_PKT_TXDONE_QUEUE 9
  64. #define NPE_PKT_MODE_HDLC 0
  65. #define NPE_PKT_MODE_RAW 1
  66. #define NPE_PKT_MODE_56KMODE 2
  67. #define NPE_PKT_MODE_56KENDIAN_MSB 4
  68. /* PKT_PIPE_HDLC_CFG_WRITE flags */
  69. #define PKT_HDLC_IDLE_ONES 0x1 /* default = flags */
  70. #define PKT_HDLC_CRC_32 0x2 /* default = CRC-16 */
  71. #define PKT_HDLC_MSB_ENDIAN 0x4 /* default = LE */
  72. /* hss_config, PCRs */
  73. /* Frame sync sampling, default = active low */
  74. #define PCR_FRM_SYNC_ACTIVE_HIGH 0x40000000
  75. #define PCR_FRM_SYNC_FALLINGEDGE 0x80000000
  76. #define PCR_FRM_SYNC_RISINGEDGE 0xC0000000
  77. /* Frame sync pin: input (default) or output generated off a given clk edge */
  78. #define PCR_FRM_SYNC_OUTPUT_FALLING 0x20000000
  79. #define PCR_FRM_SYNC_OUTPUT_RISING 0x30000000
  80. /* Frame and data clock sampling on edge, default = falling */
  81. #define PCR_FCLK_EDGE_RISING 0x08000000
  82. #define PCR_DCLK_EDGE_RISING 0x04000000
  83. /* Clock direction, default = input */
  84. #define PCR_SYNC_CLK_DIR_OUTPUT 0x02000000
  85. /* Generate/Receive frame pulses, default = enabled */
  86. #define PCR_FRM_PULSE_DISABLED 0x01000000
  87. /* Data rate is full (default) or half the configured clk speed */
  88. #define PCR_HALF_CLK_RATE 0x00200000
  89. /* Invert data between NPE and HSS FIFOs? (default = no) */
  90. #define PCR_DATA_POLARITY_INVERT 0x00100000
  91. /* TX/RX endianness, default = LSB */
  92. #define PCR_MSB_ENDIAN 0x00080000
  93. /* Normal (default) / open drain mode (TX only) */
  94. #define PCR_TX_PINS_OPEN_DRAIN 0x00040000
  95. /* No framing bit transmitted and expected on RX? (default = framing bit) */
  96. #define PCR_SOF_NO_FBIT 0x00020000
  97. /* Drive data pins? */
  98. #define PCR_TX_DATA_ENABLE 0x00010000
  99. /* Voice 56k type: drive the data pins low (default), high, high Z */
  100. #define PCR_TX_V56K_HIGH 0x00002000
  101. #define PCR_TX_V56K_HIGH_IMP 0x00004000
  102. /* Unassigned type: drive the data pins low (default), high, high Z */
  103. #define PCR_TX_UNASS_HIGH 0x00000800
  104. #define PCR_TX_UNASS_HIGH_IMP 0x00001000
  105. /* T1 @ 1.544MHz only: Fbit dictated in FIFO (default) or high Z */
  106. #define PCR_TX_FB_HIGH_IMP 0x00000400
  107. /* 56k data endiannes - which bit unused: high (default) or low */
  108. #define PCR_TX_56KE_BIT_0_UNUSED 0x00000200
  109. /* 56k data transmission type: 32/8 bit data (default) or 56K data */
  110. #define PCR_TX_56KS_56K_DATA 0x00000100
  111. /* hss_config, cCR */
  112. /* Number of packetized clients, default = 1 */
  113. #define CCR_NPE_HFIFO_2_HDLC 0x04000000
  114. #define CCR_NPE_HFIFO_3_OR_4HDLC 0x08000000
  115. /* default = no loopback */
  116. #define CCR_LOOPBACK 0x02000000
  117. /* HSS number, default = 0 (first) */
  118. #define CCR_SECOND_HSS 0x01000000
  119. /* hss_config, clkCR: main:10, num:10, denom:12 */
  120. #define CLK42X_SPEED_EXP ((0x3FF << 22) | ( 2 << 12) | 15) /*65 KHz*/
  121. #define CLK42X_SPEED_512KHZ (( 130 << 22) | ( 2 << 12) | 15)
  122. #define CLK42X_SPEED_1536KHZ (( 43 << 22) | ( 18 << 12) | 47)
  123. #define CLK42X_SPEED_1544KHZ (( 43 << 22) | ( 33 << 12) | 192)
  124. #define CLK42X_SPEED_2048KHZ (( 32 << 22) | ( 34 << 12) | 63)
  125. #define CLK42X_SPEED_4096KHZ (( 16 << 22) | ( 34 << 12) | 127)
  126. #define CLK42X_SPEED_8192KHZ (( 8 << 22) | ( 34 << 12) | 255)
  127. #define CLK46X_SPEED_512KHZ (( 130 << 22) | ( 24 << 12) | 127)
  128. #define CLK46X_SPEED_1536KHZ (( 43 << 22) | (152 << 12) | 383)
  129. #define CLK46X_SPEED_1544KHZ (( 43 << 22) | ( 66 << 12) | 385)
  130. #define CLK46X_SPEED_2048KHZ (( 32 << 22) | (280 << 12) | 511)
  131. #define CLK46X_SPEED_4096KHZ (( 16 << 22) | (280 << 12) | 1023)
  132. #define CLK46X_SPEED_8192KHZ (( 8 << 22) | (280 << 12) | 2047)
  133. /*
  134. * HSS_CONFIG_CLOCK_CR register consists of 3 parts:
  135. * A (10 bits), B (10 bits) and C (12 bits).
  136. * IXP42x HSS clock generator operation (verified with an oscilloscope):
  137. * Each clock bit takes 7.5 ns (1 / 133.xx MHz).
  138. * The clock sequence consists of (C - B) states of 0s and 1s, each state is
  139. * A bits wide. It's followed by (B + 1) states of 0s and 1s, each state is
  140. * (A + 1) bits wide.
  141. *
  142. * The resulting average clock frequency (assuming 33.333 MHz oscillator) is:
  143. * freq = 66.666 MHz / (A + (B + 1) / (C + 1))
  144. * minimum freq = 66.666 MHz / (A + 1)
  145. * maximum freq = 66.666 MHz / A
  146. *
  147. * Example: A = 2, B = 2, C = 7, CLOCK_CR register = 2 << 22 | 2 << 12 | 7
  148. * freq = 66.666 MHz / (2 + (2 + 1) / (7 + 1)) = 28.07 MHz (Mb/s).
  149. * The clock sequence is: 1100110011 (5 doubles) 000111000 (3 triples).
  150. * The sequence takes (C - B) * A + (B + 1) * (A + 1) = 5 * 2 + 3 * 3 bits
  151. * = 19 bits (each 7.5 ns long) = 142.5 ns (then the sequence repeats).
  152. * The sequence consists of 4 complete clock periods, thus the average
  153. * frequency (= clock rate) is 4 / 142.5 ns = 28.07 MHz (Mb/s).
  154. * (max specified clock rate for IXP42x HSS is 8.192 Mb/s).
  155. */
  156. /* hss_config, LUT entries */
  157. #define TDMMAP_UNASSIGNED 0
  158. #define TDMMAP_HDLC 1 /* HDLC - packetized */
  159. #define TDMMAP_VOICE56K 2 /* Voice56K - 7-bit channelized */
  160. #define TDMMAP_VOICE64K 3 /* Voice64K - 8-bit channelized */
  161. /* offsets into HSS config */
  162. #define HSS_CONFIG_TX_PCR 0x00 /* port configuration registers */
  163. #define HSS_CONFIG_RX_PCR 0x04
  164. #define HSS_CONFIG_CORE_CR 0x08 /* loopback control, HSS# */
  165. #define HSS_CONFIG_CLOCK_CR 0x0C /* clock generator control */
  166. #define HSS_CONFIG_TX_FCR 0x10 /* frame configuration registers */
  167. #define HSS_CONFIG_RX_FCR 0x14
  168. #define HSS_CONFIG_TX_LUT 0x18 /* channel look-up tables */
  169. #define HSS_CONFIG_RX_LUT 0x38
  170. /* NPE command codes */
  171. /* writes the ConfigWord value to the location specified by offset */
  172. #define PORT_CONFIG_WRITE 0x40
  173. /* triggers the NPE to load the contents of the configuration table */
  174. #define PORT_CONFIG_LOAD 0x41
  175. /* triggers the NPE to return an HssErrorReadResponse message */
  176. #define PORT_ERROR_READ 0x42
  177. /* triggers the NPE to reset internal status and enable the HssPacketized
  178. operation for the flow specified by pPipe */
  179. #define PKT_PIPE_FLOW_ENABLE 0x50
  180. #define PKT_PIPE_FLOW_DISABLE 0x51
  181. #define PKT_NUM_PIPES_WRITE 0x52
  182. #define PKT_PIPE_FIFO_SIZEW_WRITE 0x53
  183. #define PKT_PIPE_HDLC_CFG_WRITE 0x54
  184. #define PKT_PIPE_IDLE_PATTERN_WRITE 0x55
  185. #define PKT_PIPE_RX_SIZE_WRITE 0x56
  186. #define PKT_PIPE_MODE_WRITE 0x57
  187. /* HDLC packet status values - desc->status */
  188. #define ERR_SHUTDOWN 1 /* stop or shutdown occurrence */
  189. #define ERR_HDLC_ALIGN 2 /* HDLC alignment error */
  190. #define ERR_HDLC_FCS 3 /* HDLC Frame Check Sum error */
  191. #define ERR_RXFREE_Q_EMPTY 4 /* RX-free queue became empty while receiving
  192. this packet (if buf_len < pkt_len) */
  193. #define ERR_HDLC_TOO_LONG 5 /* HDLC frame size too long */
  194. #define ERR_HDLC_ABORT 6 /* abort sequence received */
  195. #define ERR_DISCONNECTING 7 /* disconnect is in progress */
  196. #ifdef __ARMEB__
  197. typedef struct sk_buff buffer_t;
  198. #define free_buffer dev_kfree_skb
  199. #define free_buffer_irq dev_consume_skb_irq
  200. #else
  201. typedef void buffer_t;
  202. #define free_buffer kfree
  203. #define free_buffer_irq kfree
  204. #endif
  205. struct port {
  206. struct device *dev;
  207. struct npe *npe;
  208. struct net_device *netdev;
  209. struct napi_struct napi;
  210. struct hss_plat_info *plat;
  211. buffer_t *rx_buff_tab[RX_DESCS], *tx_buff_tab[TX_DESCS];
  212. struct desc *desc_tab; /* coherent */
  213. dma_addr_t desc_tab_phys;
  214. unsigned int id;
  215. unsigned int clock_type, clock_rate, loopback;
  216. unsigned int initialized, carrier;
  217. u8 hdlc_cfg;
  218. u32 clock_reg;
  219. };
  220. /* NPE message structure */
  221. struct msg {
  222. #ifdef __ARMEB__
  223. u8 cmd, unused, hss_port, index;
  224. union {
  225. struct { u8 data8a, data8b, data8c, data8d; };
  226. struct { u16 data16a, data16b; };
  227. struct { u32 data32; };
  228. };
  229. #else
  230. u8 index, hss_port, unused, cmd;
  231. union {
  232. struct { u8 data8d, data8c, data8b, data8a; };
  233. struct { u16 data16b, data16a; };
  234. struct { u32 data32; };
  235. };
  236. #endif
  237. };
  238. /* HDLC packet descriptor */
  239. struct desc {
  240. u32 next; /* pointer to next buffer, unused */
  241. #ifdef __ARMEB__
  242. u16 buf_len; /* buffer length */
  243. u16 pkt_len; /* packet length */
  244. u32 data; /* pointer to data buffer in RAM */
  245. u8 status;
  246. u8 error_count;
  247. u16 __reserved;
  248. #else
  249. u16 pkt_len; /* packet length */
  250. u16 buf_len; /* buffer length */
  251. u32 data; /* pointer to data buffer in RAM */
  252. u16 __reserved;
  253. u8 error_count;
  254. u8 status;
  255. #endif
  256. u32 __reserved1[4];
  257. };
  258. #define rx_desc_phys(port, n) ((port)->desc_tab_phys + \
  259. (n) * sizeof(struct desc))
  260. #define rx_desc_ptr(port, n) (&(port)->desc_tab[n])
  261. #define tx_desc_phys(port, n) ((port)->desc_tab_phys + \
  262. ((n) + RX_DESCS) * sizeof(struct desc))
  263. #define tx_desc_ptr(port, n) (&(port)->desc_tab[(n) + RX_DESCS])
  264. /*****************************************************************************
  265. * global variables
  266. ****************************************************************************/
  267. static int ports_open;
  268. static struct dma_pool *dma_pool;
  269. static spinlock_t npe_lock;
  270. static const struct {
  271. int tx, txdone, rx, rxfree;
  272. }queue_ids[2] = {{HSS0_PKT_TX0_QUEUE, HSS0_PKT_TXDONE_QUEUE, HSS0_PKT_RX_QUEUE,
  273. HSS0_PKT_RXFREE0_QUEUE},
  274. {HSS1_PKT_TX0_QUEUE, HSS1_PKT_TXDONE_QUEUE, HSS1_PKT_RX_QUEUE,
  275. HSS1_PKT_RXFREE0_QUEUE},
  276. };
  277. /*****************************************************************************
  278. * utility functions
  279. ****************************************************************************/
  280. static inline struct port* dev_to_port(struct net_device *dev)
  281. {
  282. return dev_to_hdlc(dev)->priv;
  283. }
  284. #ifndef __ARMEB__
  285. static inline void memcpy_swab32(u32 *dest, u32 *src, int cnt)
  286. {
  287. int i;
  288. for (i = 0; i < cnt; i++)
  289. dest[i] = swab32(src[i]);
  290. }
  291. #endif
  292. /*****************************************************************************
  293. * HSS access
  294. ****************************************************************************/
  295. static void hss_npe_send(struct port *port, struct msg *msg, const char* what)
  296. {
  297. u32 *val = (u32*)msg;
  298. if (npe_send_message(port->npe, msg, what)) {
  299. pr_crit("HSS-%i: unable to send command [%08X:%08X] to %s\n",
  300. port->id, val[0], val[1], npe_name(port->npe));
  301. BUG();
  302. }
  303. }
  304. static void hss_config_set_lut(struct port *port)
  305. {
  306. struct msg msg;
  307. int ch;
  308. memset(&msg, 0, sizeof(msg));
  309. msg.cmd = PORT_CONFIG_WRITE;
  310. msg.hss_port = port->id;
  311. for (ch = 0; ch < MAX_CHANNELS; ch++) {
  312. msg.data32 >>= 2;
  313. msg.data32 |= TDMMAP_HDLC << 30;
  314. if (ch % 16 == 15) {
  315. msg.index = HSS_CONFIG_TX_LUT + ((ch / 4) & ~3);
  316. hss_npe_send(port, &msg, "HSS_SET_TX_LUT");
  317. msg.index += HSS_CONFIG_RX_LUT - HSS_CONFIG_TX_LUT;
  318. hss_npe_send(port, &msg, "HSS_SET_RX_LUT");
  319. }
  320. }
  321. }
  322. static void hss_config(struct port *port)
  323. {
  324. struct msg msg;
  325. memset(&msg, 0, sizeof(msg));
  326. msg.cmd = PORT_CONFIG_WRITE;
  327. msg.hss_port = port->id;
  328. msg.index = HSS_CONFIG_TX_PCR;
  329. msg.data32 = PCR_FRM_PULSE_DISABLED | PCR_MSB_ENDIAN |
  330. PCR_TX_DATA_ENABLE | PCR_SOF_NO_FBIT;
  331. if (port->clock_type == CLOCK_INT)
  332. msg.data32 |= PCR_SYNC_CLK_DIR_OUTPUT;
  333. hss_npe_send(port, &msg, "HSS_SET_TX_PCR");
  334. msg.index = HSS_CONFIG_RX_PCR;
  335. msg.data32 ^= PCR_TX_DATA_ENABLE | PCR_DCLK_EDGE_RISING;
  336. hss_npe_send(port, &msg, "HSS_SET_RX_PCR");
  337. memset(&msg, 0, sizeof(msg));
  338. msg.cmd = PORT_CONFIG_WRITE;
  339. msg.hss_port = port->id;
  340. msg.index = HSS_CONFIG_CORE_CR;
  341. msg.data32 = (port->loopback ? CCR_LOOPBACK : 0) |
  342. (port->id ? CCR_SECOND_HSS : 0);
  343. hss_npe_send(port, &msg, "HSS_SET_CORE_CR");
  344. memset(&msg, 0, sizeof(msg));
  345. msg.cmd = PORT_CONFIG_WRITE;
  346. msg.hss_port = port->id;
  347. msg.index = HSS_CONFIG_CLOCK_CR;
  348. msg.data32 = port->clock_reg;
  349. hss_npe_send(port, &msg, "HSS_SET_CLOCK_CR");
  350. memset(&msg, 0, sizeof(msg));
  351. msg.cmd = PORT_CONFIG_WRITE;
  352. msg.hss_port = port->id;
  353. msg.index = HSS_CONFIG_TX_FCR;
  354. msg.data16a = FRAME_OFFSET;
  355. msg.data16b = FRAME_SIZE - 1;
  356. hss_npe_send(port, &msg, "HSS_SET_TX_FCR");
  357. memset(&msg, 0, sizeof(msg));
  358. msg.cmd = PORT_CONFIG_WRITE;
  359. msg.hss_port = port->id;
  360. msg.index = HSS_CONFIG_RX_FCR;
  361. msg.data16a = FRAME_OFFSET;
  362. msg.data16b = FRAME_SIZE - 1;
  363. hss_npe_send(port, &msg, "HSS_SET_RX_FCR");
  364. hss_config_set_lut(port);
  365. memset(&msg, 0, sizeof(msg));
  366. msg.cmd = PORT_CONFIG_LOAD;
  367. msg.hss_port = port->id;
  368. hss_npe_send(port, &msg, "HSS_LOAD_CONFIG");
  369. if (npe_recv_message(port->npe, &msg, "HSS_LOAD_CONFIG") ||
  370. /* HSS_LOAD_CONFIG for port #1 returns port_id = #4 */
  371. msg.cmd != PORT_CONFIG_LOAD || msg.data32) {
  372. pr_crit("HSS-%i: HSS_LOAD_CONFIG failed\n", port->id);
  373. BUG();
  374. }
  375. /* HDLC may stop working without this - check FIXME */
  376. npe_recv_message(port->npe, &msg, "FLUSH_IT");
  377. }
  378. static void hss_set_hdlc_cfg(struct port *port)
  379. {
  380. struct msg msg;
  381. memset(&msg, 0, sizeof(msg));
  382. msg.cmd = PKT_PIPE_HDLC_CFG_WRITE;
  383. msg.hss_port = port->id;
  384. msg.data8a = port->hdlc_cfg; /* rx_cfg */
  385. msg.data8b = port->hdlc_cfg | (PKT_EXTRA_FLAGS << 3); /* tx_cfg */
  386. hss_npe_send(port, &msg, "HSS_SET_HDLC_CFG");
  387. }
  388. static u32 hss_get_status(struct port *port)
  389. {
  390. struct msg msg;
  391. memset(&msg, 0, sizeof(msg));
  392. msg.cmd = PORT_ERROR_READ;
  393. msg.hss_port = port->id;
  394. hss_npe_send(port, &msg, "PORT_ERROR_READ");
  395. if (npe_recv_message(port->npe, &msg, "PORT_ERROR_READ")) {
  396. pr_crit("HSS-%i: unable to read HSS status\n", port->id);
  397. BUG();
  398. }
  399. return msg.data32;
  400. }
  401. static void hss_start_hdlc(struct port *port)
  402. {
  403. struct msg msg;
  404. memset(&msg, 0, sizeof(msg));
  405. msg.cmd = PKT_PIPE_FLOW_ENABLE;
  406. msg.hss_port = port->id;
  407. msg.data32 = 0;
  408. hss_npe_send(port, &msg, "HSS_ENABLE_PKT_PIPE");
  409. }
  410. static void hss_stop_hdlc(struct port *port)
  411. {
  412. struct msg msg;
  413. memset(&msg, 0, sizeof(msg));
  414. msg.cmd = PKT_PIPE_FLOW_DISABLE;
  415. msg.hss_port = port->id;
  416. hss_npe_send(port, &msg, "HSS_DISABLE_PKT_PIPE");
  417. hss_get_status(port); /* make sure it's halted */
  418. }
  419. static int hss_load_firmware(struct port *port)
  420. {
  421. struct msg msg;
  422. int err;
  423. if (port->initialized)
  424. return 0;
  425. if (!npe_running(port->npe) &&
  426. (err = npe_load_firmware(port->npe, npe_name(port->npe),
  427. port->dev)))
  428. return err;
  429. /* HDLC mode configuration */
  430. memset(&msg, 0, sizeof(msg));
  431. msg.cmd = PKT_NUM_PIPES_WRITE;
  432. msg.hss_port = port->id;
  433. msg.data8a = PKT_NUM_PIPES;
  434. hss_npe_send(port, &msg, "HSS_SET_PKT_PIPES");
  435. msg.cmd = PKT_PIPE_FIFO_SIZEW_WRITE;
  436. msg.data8a = PKT_PIPE_FIFO_SIZEW;
  437. hss_npe_send(port, &msg, "HSS_SET_PKT_FIFO");
  438. msg.cmd = PKT_PIPE_MODE_WRITE;
  439. msg.data8a = NPE_PKT_MODE_HDLC;
  440. /* msg.data8b = inv_mask */
  441. /* msg.data8c = or_mask */
  442. hss_npe_send(port, &msg, "HSS_SET_PKT_MODE");
  443. msg.cmd = PKT_PIPE_RX_SIZE_WRITE;
  444. msg.data16a = HDLC_MAX_MRU; /* including CRC */
  445. hss_npe_send(port, &msg, "HSS_SET_PKT_RX_SIZE");
  446. msg.cmd = PKT_PIPE_IDLE_PATTERN_WRITE;
  447. msg.data32 = 0x7F7F7F7F; /* ??? FIXME */
  448. hss_npe_send(port, &msg, "HSS_SET_PKT_IDLE");
  449. port->initialized = 1;
  450. return 0;
  451. }
  452. /*****************************************************************************
  453. * packetized (HDLC) operation
  454. ****************************************************************************/
  455. static inline void debug_pkt(struct net_device *dev, const char *func,
  456. u8 *data, int len)
  457. {
  458. #if DEBUG_PKT_BYTES
  459. int i;
  460. printk(KERN_DEBUG "%s: %s(%i)", dev->name, func, len);
  461. for (i = 0; i < len; i++) {
  462. if (i >= DEBUG_PKT_BYTES)
  463. break;
  464. printk("%s%02X", !(i % 4) ? " " : "", data[i]);
  465. }
  466. printk("\n");
  467. #endif
  468. }
  469. static inline void debug_desc(u32 phys, struct desc *desc)
  470. {
  471. #if DEBUG_DESC
  472. printk(KERN_DEBUG "%X: %X %3X %3X %08X %X %X\n",
  473. phys, desc->next, desc->buf_len, desc->pkt_len,
  474. desc->data, desc->status, desc->error_count);
  475. #endif
  476. }
  477. static inline int queue_get_desc(unsigned int queue, struct port *port,
  478. int is_tx)
  479. {
  480. u32 phys, tab_phys, n_desc;
  481. struct desc *tab;
  482. if (!(phys = qmgr_get_entry(queue)))
  483. return -1;
  484. BUG_ON(phys & 0x1F);
  485. tab_phys = is_tx ? tx_desc_phys(port, 0) : rx_desc_phys(port, 0);
  486. tab = is_tx ? tx_desc_ptr(port, 0) : rx_desc_ptr(port, 0);
  487. n_desc = (phys - tab_phys) / sizeof(struct desc);
  488. BUG_ON(n_desc >= (is_tx ? TX_DESCS : RX_DESCS));
  489. debug_desc(phys, &tab[n_desc]);
  490. BUG_ON(tab[n_desc].next);
  491. return n_desc;
  492. }
  493. static inline void queue_put_desc(unsigned int queue, u32 phys,
  494. struct desc *desc)
  495. {
  496. debug_desc(phys, desc);
  497. BUG_ON(phys & 0x1F);
  498. qmgr_put_entry(queue, phys);
  499. /* Don't check for queue overflow here, we've allocated sufficient
  500. length and queues >= 32 don't support this check anyway. */
  501. }
  502. static inline void dma_unmap_tx(struct port *port, struct desc *desc)
  503. {
  504. #ifdef __ARMEB__
  505. dma_unmap_single(&port->netdev->dev, desc->data,
  506. desc->buf_len, DMA_TO_DEVICE);
  507. #else
  508. dma_unmap_single(&port->netdev->dev, desc->data & ~3,
  509. ALIGN((desc->data & 3) + desc->buf_len, 4),
  510. DMA_TO_DEVICE);
  511. #endif
  512. }
  513. static void hss_hdlc_set_carrier(void *pdev, int carrier)
  514. {
  515. struct net_device *netdev = pdev;
  516. struct port *port = dev_to_port(netdev);
  517. unsigned long flags;
  518. spin_lock_irqsave(&npe_lock, flags);
  519. port->carrier = carrier;
  520. if (!port->loopback) {
  521. if (carrier)
  522. netif_carrier_on(netdev);
  523. else
  524. netif_carrier_off(netdev);
  525. }
  526. spin_unlock_irqrestore(&npe_lock, flags);
  527. }
  528. static void hss_hdlc_rx_irq(void *pdev)
  529. {
  530. struct net_device *dev = pdev;
  531. struct port *port = dev_to_port(dev);
  532. #if DEBUG_RX
  533. printk(KERN_DEBUG "%s: hss_hdlc_rx_irq\n", dev->name);
  534. #endif
  535. qmgr_disable_irq(queue_ids[port->id].rx);
  536. napi_schedule(&port->napi);
  537. }
  538. static int hss_hdlc_poll(struct napi_struct *napi, int budget)
  539. {
  540. struct port *port = container_of(napi, struct port, napi);
  541. struct net_device *dev = port->netdev;
  542. unsigned int rxq = queue_ids[port->id].rx;
  543. unsigned int rxfreeq = queue_ids[port->id].rxfree;
  544. int received = 0;
  545. #if DEBUG_RX
  546. printk(KERN_DEBUG "%s: hss_hdlc_poll\n", dev->name);
  547. #endif
  548. while (received < budget) {
  549. struct sk_buff *skb;
  550. struct desc *desc;
  551. int n;
  552. #ifdef __ARMEB__
  553. struct sk_buff *temp;
  554. u32 phys;
  555. #endif
  556. if ((n = queue_get_desc(rxq, port, 0)) < 0) {
  557. #if DEBUG_RX
  558. printk(KERN_DEBUG "%s: hss_hdlc_poll"
  559. " napi_complete\n", dev->name);
  560. #endif
  561. napi_complete(napi);
  562. qmgr_enable_irq(rxq);
  563. if (!qmgr_stat_empty(rxq) &&
  564. napi_reschedule(napi)) {
  565. #if DEBUG_RX
  566. printk(KERN_DEBUG "%s: hss_hdlc_poll"
  567. " napi_reschedule succeeded\n",
  568. dev->name);
  569. #endif
  570. qmgr_disable_irq(rxq);
  571. continue;
  572. }
  573. #if DEBUG_RX
  574. printk(KERN_DEBUG "%s: hss_hdlc_poll all done\n",
  575. dev->name);
  576. #endif
  577. return received; /* all work done */
  578. }
  579. desc = rx_desc_ptr(port, n);
  580. #if 0 /* FIXME - error_count counts modulo 256, perhaps we should use it */
  581. if (desc->error_count)
  582. printk(KERN_DEBUG "%s: hss_hdlc_poll status 0x%02X"
  583. " errors %u\n", dev->name, desc->status,
  584. desc->error_count);
  585. #endif
  586. skb = NULL;
  587. switch (desc->status) {
  588. case 0:
  589. #ifdef __ARMEB__
  590. if ((skb = netdev_alloc_skb(dev, RX_SIZE)) != NULL) {
  591. phys = dma_map_single(&dev->dev, skb->data,
  592. RX_SIZE,
  593. DMA_FROM_DEVICE);
  594. if (dma_mapping_error(&dev->dev, phys)) {
  595. dev_kfree_skb(skb);
  596. skb = NULL;
  597. }
  598. }
  599. #else
  600. skb = netdev_alloc_skb(dev, desc->pkt_len);
  601. #endif
  602. if (!skb)
  603. dev->stats.rx_dropped++;
  604. break;
  605. case ERR_HDLC_ALIGN:
  606. case ERR_HDLC_ABORT:
  607. dev->stats.rx_frame_errors++;
  608. dev->stats.rx_errors++;
  609. break;
  610. case ERR_HDLC_FCS:
  611. dev->stats.rx_crc_errors++;
  612. dev->stats.rx_errors++;
  613. break;
  614. case ERR_HDLC_TOO_LONG:
  615. dev->stats.rx_length_errors++;
  616. dev->stats.rx_errors++;
  617. break;
  618. default: /* FIXME - remove printk */
  619. netdev_err(dev, "hss_hdlc_poll: status 0x%02X errors %u\n",
  620. desc->status, desc->error_count);
  621. dev->stats.rx_errors++;
  622. }
  623. if (!skb) {
  624. /* put the desc back on RX-ready queue */
  625. desc->buf_len = RX_SIZE;
  626. desc->pkt_len = desc->status = 0;
  627. queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
  628. continue;
  629. }
  630. /* process received frame */
  631. #ifdef __ARMEB__
  632. temp = skb;
  633. skb = port->rx_buff_tab[n];
  634. dma_unmap_single(&dev->dev, desc->data,
  635. RX_SIZE, DMA_FROM_DEVICE);
  636. #else
  637. dma_sync_single_for_cpu(&dev->dev, desc->data,
  638. RX_SIZE, DMA_FROM_DEVICE);
  639. memcpy_swab32((u32 *)skb->data, (u32 *)port->rx_buff_tab[n],
  640. ALIGN(desc->pkt_len, 4) / 4);
  641. #endif
  642. skb_put(skb, desc->pkt_len);
  643. debug_pkt(dev, "hss_hdlc_poll", skb->data, skb->len);
  644. skb->protocol = hdlc_type_trans(skb, dev);
  645. dev->stats.rx_packets++;
  646. dev->stats.rx_bytes += skb->len;
  647. netif_receive_skb(skb);
  648. /* put the new buffer on RX-free queue */
  649. #ifdef __ARMEB__
  650. port->rx_buff_tab[n] = temp;
  651. desc->data = phys;
  652. #endif
  653. desc->buf_len = RX_SIZE;
  654. desc->pkt_len = 0;
  655. queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
  656. received++;
  657. }
  658. #if DEBUG_RX
  659. printk(KERN_DEBUG "hss_hdlc_poll: end, not all work done\n");
  660. #endif
  661. return received; /* not all work done */
  662. }
  663. static void hss_hdlc_txdone_irq(void *pdev)
  664. {
  665. struct net_device *dev = pdev;
  666. struct port *port = dev_to_port(dev);
  667. int n_desc;
  668. #if DEBUG_TX
  669. printk(KERN_DEBUG DRV_NAME ": hss_hdlc_txdone_irq\n");
  670. #endif
  671. while ((n_desc = queue_get_desc(queue_ids[port->id].txdone,
  672. port, 1)) >= 0) {
  673. struct desc *desc;
  674. int start;
  675. desc = tx_desc_ptr(port, n_desc);
  676. dev->stats.tx_packets++;
  677. dev->stats.tx_bytes += desc->pkt_len;
  678. dma_unmap_tx(port, desc);
  679. #if DEBUG_TX
  680. printk(KERN_DEBUG "%s: hss_hdlc_txdone_irq free %p\n",
  681. dev->name, port->tx_buff_tab[n_desc]);
  682. #endif
  683. free_buffer_irq(port->tx_buff_tab[n_desc]);
  684. port->tx_buff_tab[n_desc] = NULL;
  685. start = qmgr_stat_below_low_watermark(port->plat->txreadyq);
  686. queue_put_desc(port->plat->txreadyq,
  687. tx_desc_phys(port, n_desc), desc);
  688. if (start) { /* TX-ready queue was empty */
  689. #if DEBUG_TX
  690. printk(KERN_DEBUG "%s: hss_hdlc_txdone_irq xmit"
  691. " ready\n", dev->name);
  692. #endif
  693. netif_wake_queue(dev);
  694. }
  695. }
  696. }
  697. static int hss_hdlc_xmit(struct sk_buff *skb, struct net_device *dev)
  698. {
  699. struct port *port = dev_to_port(dev);
  700. unsigned int txreadyq = port->plat->txreadyq;
  701. int len, offset, bytes, n;
  702. void *mem;
  703. u32 phys;
  704. struct desc *desc;
  705. #if DEBUG_TX
  706. printk(KERN_DEBUG "%s: hss_hdlc_xmit\n", dev->name);
  707. #endif
  708. if (unlikely(skb->len > HDLC_MAX_MRU)) {
  709. dev_kfree_skb(skb);
  710. dev->stats.tx_errors++;
  711. return NETDEV_TX_OK;
  712. }
  713. debug_pkt(dev, "hss_hdlc_xmit", skb->data, skb->len);
  714. len = skb->len;
  715. #ifdef __ARMEB__
  716. offset = 0; /* no need to keep alignment */
  717. bytes = len;
  718. mem = skb->data;
  719. #else
  720. offset = (int)skb->data & 3; /* keep 32-bit alignment */
  721. bytes = ALIGN(offset + len, 4);
  722. if (!(mem = kmalloc(bytes, GFP_ATOMIC))) {
  723. dev_kfree_skb(skb);
  724. dev->stats.tx_dropped++;
  725. return NETDEV_TX_OK;
  726. }
  727. memcpy_swab32(mem, (u32 *)((uintptr_t)skb->data & ~3), bytes / 4);
  728. dev_kfree_skb(skb);
  729. #endif
  730. phys = dma_map_single(&dev->dev, mem, bytes, DMA_TO_DEVICE);
  731. if (dma_mapping_error(&dev->dev, phys)) {
  732. #ifdef __ARMEB__
  733. dev_kfree_skb(skb);
  734. #else
  735. kfree(mem);
  736. #endif
  737. dev->stats.tx_dropped++;
  738. return NETDEV_TX_OK;
  739. }
  740. n = queue_get_desc(txreadyq, port, 1);
  741. BUG_ON(n < 0);
  742. desc = tx_desc_ptr(port, n);
  743. #ifdef __ARMEB__
  744. port->tx_buff_tab[n] = skb;
  745. #else
  746. port->tx_buff_tab[n] = mem;
  747. #endif
  748. desc->data = phys + offset;
  749. desc->buf_len = desc->pkt_len = len;
  750. wmb();
  751. queue_put_desc(queue_ids[port->id].tx, tx_desc_phys(port, n), desc);
  752. if (qmgr_stat_below_low_watermark(txreadyq)) { /* empty */
  753. #if DEBUG_TX
  754. printk(KERN_DEBUG "%s: hss_hdlc_xmit queue full\n", dev->name);
  755. #endif
  756. netif_stop_queue(dev);
  757. /* we could miss TX ready interrupt */
  758. if (!qmgr_stat_below_low_watermark(txreadyq)) {
  759. #if DEBUG_TX
  760. printk(KERN_DEBUG "%s: hss_hdlc_xmit ready again\n",
  761. dev->name);
  762. #endif
  763. netif_wake_queue(dev);
  764. }
  765. }
  766. #if DEBUG_TX
  767. printk(KERN_DEBUG "%s: hss_hdlc_xmit end\n", dev->name);
  768. #endif
  769. return NETDEV_TX_OK;
  770. }
  771. static int request_hdlc_queues(struct port *port)
  772. {
  773. int err;
  774. err = qmgr_request_queue(queue_ids[port->id].rxfree, RX_DESCS, 0, 0,
  775. "%s:RX-free", port->netdev->name);
  776. if (err)
  777. return err;
  778. err = qmgr_request_queue(queue_ids[port->id].rx, RX_DESCS, 0, 0,
  779. "%s:RX", port->netdev->name);
  780. if (err)
  781. goto rel_rxfree;
  782. err = qmgr_request_queue(queue_ids[port->id].tx, TX_DESCS, 0, 0,
  783. "%s:TX", port->netdev->name);
  784. if (err)
  785. goto rel_rx;
  786. err = qmgr_request_queue(port->plat->txreadyq, TX_DESCS, 0, 0,
  787. "%s:TX-ready", port->netdev->name);
  788. if (err)
  789. goto rel_tx;
  790. err = qmgr_request_queue(queue_ids[port->id].txdone, TX_DESCS, 0, 0,
  791. "%s:TX-done", port->netdev->name);
  792. if (err)
  793. goto rel_txready;
  794. return 0;
  795. rel_txready:
  796. qmgr_release_queue(port->plat->txreadyq);
  797. rel_tx:
  798. qmgr_release_queue(queue_ids[port->id].tx);
  799. rel_rx:
  800. qmgr_release_queue(queue_ids[port->id].rx);
  801. rel_rxfree:
  802. qmgr_release_queue(queue_ids[port->id].rxfree);
  803. printk(KERN_DEBUG "%s: unable to request hardware queues\n",
  804. port->netdev->name);
  805. return err;
  806. }
  807. static void release_hdlc_queues(struct port *port)
  808. {
  809. qmgr_release_queue(queue_ids[port->id].rxfree);
  810. qmgr_release_queue(queue_ids[port->id].rx);
  811. qmgr_release_queue(queue_ids[port->id].txdone);
  812. qmgr_release_queue(queue_ids[port->id].tx);
  813. qmgr_release_queue(port->plat->txreadyq);
  814. }
  815. static int init_hdlc_queues(struct port *port)
  816. {
  817. int i;
  818. if (!ports_open) {
  819. dma_pool = dma_pool_create(DRV_NAME, &port->netdev->dev,
  820. POOL_ALLOC_SIZE, 32, 0);
  821. if (!dma_pool)
  822. return -ENOMEM;
  823. }
  824. if (!(port->desc_tab = dma_pool_alloc(dma_pool, GFP_KERNEL,
  825. &port->desc_tab_phys)))
  826. return -ENOMEM;
  827. memset(port->desc_tab, 0, POOL_ALLOC_SIZE);
  828. memset(port->rx_buff_tab, 0, sizeof(port->rx_buff_tab)); /* tables */
  829. memset(port->tx_buff_tab, 0, sizeof(port->tx_buff_tab));
  830. /* Setup RX buffers */
  831. for (i = 0; i < RX_DESCS; i++) {
  832. struct desc *desc = rx_desc_ptr(port, i);
  833. buffer_t *buff;
  834. void *data;
  835. #ifdef __ARMEB__
  836. if (!(buff = netdev_alloc_skb(port->netdev, RX_SIZE)))
  837. return -ENOMEM;
  838. data = buff->data;
  839. #else
  840. if (!(buff = kmalloc(RX_SIZE, GFP_KERNEL)))
  841. return -ENOMEM;
  842. data = buff;
  843. #endif
  844. desc->buf_len = RX_SIZE;
  845. desc->data = dma_map_single(&port->netdev->dev, data,
  846. RX_SIZE, DMA_FROM_DEVICE);
  847. if (dma_mapping_error(&port->netdev->dev, desc->data)) {
  848. free_buffer(buff);
  849. return -EIO;
  850. }
  851. port->rx_buff_tab[i] = buff;
  852. }
  853. return 0;
  854. }
  855. static void destroy_hdlc_queues(struct port *port)
  856. {
  857. int i;
  858. if (port->desc_tab) {
  859. for (i = 0; i < RX_DESCS; i++) {
  860. struct desc *desc = rx_desc_ptr(port, i);
  861. buffer_t *buff = port->rx_buff_tab[i];
  862. if (buff) {
  863. dma_unmap_single(&port->netdev->dev,
  864. desc->data, RX_SIZE,
  865. DMA_FROM_DEVICE);
  866. free_buffer(buff);
  867. }
  868. }
  869. for (i = 0; i < TX_DESCS; i++) {
  870. struct desc *desc = tx_desc_ptr(port, i);
  871. buffer_t *buff = port->tx_buff_tab[i];
  872. if (buff) {
  873. dma_unmap_tx(port, desc);
  874. free_buffer(buff);
  875. }
  876. }
  877. dma_pool_free(dma_pool, port->desc_tab, port->desc_tab_phys);
  878. port->desc_tab = NULL;
  879. }
  880. if (!ports_open && dma_pool) {
  881. dma_pool_destroy(dma_pool);
  882. dma_pool = NULL;
  883. }
  884. }
  885. static int hss_hdlc_open(struct net_device *dev)
  886. {
  887. struct port *port = dev_to_port(dev);
  888. unsigned long flags;
  889. int i, err = 0;
  890. if ((err = hdlc_open(dev)))
  891. return err;
  892. if ((err = hss_load_firmware(port)))
  893. goto err_hdlc_close;
  894. if ((err = request_hdlc_queues(port)))
  895. goto err_hdlc_close;
  896. if ((err = init_hdlc_queues(port)))
  897. goto err_destroy_queues;
  898. spin_lock_irqsave(&npe_lock, flags);
  899. if (port->plat->open)
  900. if ((err = port->plat->open(port->id, dev,
  901. hss_hdlc_set_carrier)))
  902. goto err_unlock;
  903. spin_unlock_irqrestore(&npe_lock, flags);
  904. /* Populate queues with buffers, no failure after this point */
  905. for (i = 0; i < TX_DESCS; i++)
  906. queue_put_desc(port->plat->txreadyq,
  907. tx_desc_phys(port, i), tx_desc_ptr(port, i));
  908. for (i = 0; i < RX_DESCS; i++)
  909. queue_put_desc(queue_ids[port->id].rxfree,
  910. rx_desc_phys(port, i), rx_desc_ptr(port, i));
  911. napi_enable(&port->napi);
  912. netif_start_queue(dev);
  913. qmgr_set_irq(queue_ids[port->id].rx, QUEUE_IRQ_SRC_NOT_EMPTY,
  914. hss_hdlc_rx_irq, dev);
  915. qmgr_set_irq(queue_ids[port->id].txdone, QUEUE_IRQ_SRC_NOT_EMPTY,
  916. hss_hdlc_txdone_irq, dev);
  917. qmgr_enable_irq(queue_ids[port->id].txdone);
  918. ports_open++;
  919. hss_set_hdlc_cfg(port);
  920. hss_config(port);
  921. hss_start_hdlc(port);
  922. /* we may already have RX data, enables IRQ */
  923. napi_schedule(&port->napi);
  924. return 0;
  925. err_unlock:
  926. spin_unlock_irqrestore(&npe_lock, flags);
  927. err_destroy_queues:
  928. destroy_hdlc_queues(port);
  929. release_hdlc_queues(port);
  930. err_hdlc_close:
  931. hdlc_close(dev);
  932. return err;
  933. }
  934. static int hss_hdlc_close(struct net_device *dev)
  935. {
  936. struct port *port = dev_to_port(dev);
  937. unsigned long flags;
  938. int i, buffs = RX_DESCS; /* allocated RX buffers */
  939. spin_lock_irqsave(&npe_lock, flags);
  940. ports_open--;
  941. qmgr_disable_irq(queue_ids[port->id].rx);
  942. netif_stop_queue(dev);
  943. napi_disable(&port->napi);
  944. hss_stop_hdlc(port);
  945. while (queue_get_desc(queue_ids[port->id].rxfree, port, 0) >= 0)
  946. buffs--;
  947. while (queue_get_desc(queue_ids[port->id].rx, port, 0) >= 0)
  948. buffs--;
  949. if (buffs)
  950. netdev_crit(dev, "unable to drain RX queue, %i buffer(s) left in NPE\n",
  951. buffs);
  952. buffs = TX_DESCS;
  953. while (queue_get_desc(queue_ids[port->id].tx, port, 1) >= 0)
  954. buffs--; /* cancel TX */
  955. i = 0;
  956. do {
  957. while (queue_get_desc(port->plat->txreadyq, port, 1) >= 0)
  958. buffs--;
  959. if (!buffs)
  960. break;
  961. } while (++i < MAX_CLOSE_WAIT);
  962. if (buffs)
  963. netdev_crit(dev, "unable to drain TX queue, %i buffer(s) left in NPE\n",
  964. buffs);
  965. #if DEBUG_CLOSE
  966. if (!buffs)
  967. printk(KERN_DEBUG "Draining TX queues took %i cycles\n", i);
  968. #endif
  969. qmgr_disable_irq(queue_ids[port->id].txdone);
  970. if (port->plat->close)
  971. port->plat->close(port->id, dev);
  972. spin_unlock_irqrestore(&npe_lock, flags);
  973. destroy_hdlc_queues(port);
  974. release_hdlc_queues(port);
  975. hdlc_close(dev);
  976. return 0;
  977. }
  978. static int hss_hdlc_attach(struct net_device *dev, unsigned short encoding,
  979. unsigned short parity)
  980. {
  981. struct port *port = dev_to_port(dev);
  982. if (encoding != ENCODING_NRZ)
  983. return -EINVAL;
  984. switch(parity) {
  985. case PARITY_CRC16_PR1_CCITT:
  986. port->hdlc_cfg = 0;
  987. return 0;
  988. case PARITY_CRC32_PR1_CCITT:
  989. port->hdlc_cfg = PKT_HDLC_CRC_32;
  990. return 0;
  991. default:
  992. return -EINVAL;
  993. }
  994. }
  995. static u32 check_clock(u32 rate, u32 a, u32 b, u32 c,
  996. u32 *best, u32 *best_diff, u32 *reg)
  997. {
  998. /* a is 10-bit, b is 10-bit, c is 12-bit */
  999. u64 new_rate;
  1000. u32 new_diff;
  1001. new_rate = ixp4xx_timer_freq * (u64)(c + 1);
  1002. do_div(new_rate, a * (c + 1) + b + 1);
  1003. new_diff = abs((u32)new_rate - rate);
  1004. if (new_diff < *best_diff) {
  1005. *best = new_rate;
  1006. *best_diff = new_diff;
  1007. *reg = (a << 22) | (b << 12) | c;
  1008. }
  1009. return new_diff;
  1010. }
  1011. static void find_best_clock(u32 rate, u32 *best, u32 *reg)
  1012. {
  1013. u32 a, b, diff = 0xFFFFFFFF;
  1014. a = ixp4xx_timer_freq / rate;
  1015. if (a > 0x3FF) { /* 10-bit value - we can go as slow as ca. 65 kb/s */
  1016. check_clock(rate, 0x3FF, 1, 1, best, &diff, reg);
  1017. return;
  1018. }
  1019. if (a == 0) { /* > 66.666 MHz */
  1020. a = 1; /* minimum divider is 1 (a = 0, b = 1, c = 1) */
  1021. rate = ixp4xx_timer_freq;
  1022. }
  1023. if (rate * a == ixp4xx_timer_freq) { /* don't divide by 0 later */
  1024. check_clock(rate, a - 1, 1, 1, best, &diff, reg);
  1025. return;
  1026. }
  1027. for (b = 0; b < 0x400; b++) {
  1028. u64 c = (b + 1) * (u64)rate;
  1029. do_div(c, ixp4xx_timer_freq - rate * a);
  1030. c--;
  1031. if (c >= 0xFFF) { /* 12-bit - no need to check more 'b's */
  1032. if (b == 0 && /* also try a bit higher rate */
  1033. !check_clock(rate, a - 1, 1, 1, best, &diff, reg))
  1034. return;
  1035. check_clock(rate, a, b, 0xFFF, best, &diff, reg);
  1036. return;
  1037. }
  1038. if (!check_clock(rate, a, b, c, best, &diff, reg))
  1039. return;
  1040. if (!check_clock(rate, a, b, c + 1, best, &diff, reg))
  1041. return;
  1042. }
  1043. }
  1044. static int hss_hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1045. {
  1046. const size_t size = sizeof(sync_serial_settings);
  1047. sync_serial_settings new_line;
  1048. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  1049. struct port *port = dev_to_port(dev);
  1050. unsigned long flags;
  1051. int clk;
  1052. if (cmd != SIOCWANDEV)
  1053. return hdlc_ioctl(dev, ifr, cmd);
  1054. switch(ifr->ifr_settings.type) {
  1055. case IF_GET_IFACE:
  1056. ifr->ifr_settings.type = IF_IFACE_V35;
  1057. if (ifr->ifr_settings.size < size) {
  1058. ifr->ifr_settings.size = size; /* data size wanted */
  1059. return -ENOBUFS;
  1060. }
  1061. memset(&new_line, 0, sizeof(new_line));
  1062. new_line.clock_type = port->clock_type;
  1063. new_line.clock_rate = port->clock_rate;
  1064. new_line.loopback = port->loopback;
  1065. if (copy_to_user(line, &new_line, size))
  1066. return -EFAULT;
  1067. return 0;
  1068. case IF_IFACE_SYNC_SERIAL:
  1069. case IF_IFACE_V35:
  1070. if(!capable(CAP_NET_ADMIN))
  1071. return -EPERM;
  1072. if (copy_from_user(&new_line, line, size))
  1073. return -EFAULT;
  1074. clk = new_line.clock_type;
  1075. if (port->plat->set_clock)
  1076. clk = port->plat->set_clock(port->id, clk);
  1077. if (clk != CLOCK_EXT && clk != CLOCK_INT)
  1078. return -EINVAL; /* No such clock setting */
  1079. if (new_line.loopback != 0 && new_line.loopback != 1)
  1080. return -EINVAL;
  1081. port->clock_type = clk; /* Update settings */
  1082. if (clk == CLOCK_INT)
  1083. find_best_clock(new_line.clock_rate, &port->clock_rate,
  1084. &port->clock_reg);
  1085. else {
  1086. port->clock_rate = 0;
  1087. port->clock_reg = CLK42X_SPEED_2048KHZ;
  1088. }
  1089. port->loopback = new_line.loopback;
  1090. spin_lock_irqsave(&npe_lock, flags);
  1091. if (dev->flags & IFF_UP)
  1092. hss_config(port);
  1093. if (port->loopback || port->carrier)
  1094. netif_carrier_on(port->netdev);
  1095. else
  1096. netif_carrier_off(port->netdev);
  1097. spin_unlock_irqrestore(&npe_lock, flags);
  1098. return 0;
  1099. default:
  1100. return hdlc_ioctl(dev, ifr, cmd);
  1101. }
  1102. }
  1103. /*****************************************************************************
  1104. * initialization
  1105. ****************************************************************************/
  1106. static const struct net_device_ops hss_hdlc_ops = {
  1107. .ndo_open = hss_hdlc_open,
  1108. .ndo_stop = hss_hdlc_close,
  1109. .ndo_start_xmit = hdlc_start_xmit,
  1110. .ndo_do_ioctl = hss_hdlc_ioctl,
  1111. };
  1112. static int hss_init_one(struct platform_device *pdev)
  1113. {
  1114. struct port *port;
  1115. struct net_device *dev;
  1116. hdlc_device *hdlc;
  1117. int err;
  1118. if ((port = kzalloc(sizeof(*port), GFP_KERNEL)) == NULL)
  1119. return -ENOMEM;
  1120. if ((port->npe = npe_request(0)) == NULL) {
  1121. err = -ENODEV;
  1122. goto err_free;
  1123. }
  1124. if ((port->netdev = dev = alloc_hdlcdev(port)) == NULL) {
  1125. err = -ENOMEM;
  1126. goto err_plat;
  1127. }
  1128. SET_NETDEV_DEV(dev, &pdev->dev);
  1129. hdlc = dev_to_hdlc(dev);
  1130. hdlc->attach = hss_hdlc_attach;
  1131. hdlc->xmit = hss_hdlc_xmit;
  1132. dev->netdev_ops = &hss_hdlc_ops;
  1133. dev->tx_queue_len = 100;
  1134. port->clock_type = CLOCK_EXT;
  1135. port->clock_rate = 0;
  1136. port->clock_reg = CLK42X_SPEED_2048KHZ;
  1137. port->id = pdev->id;
  1138. port->dev = &pdev->dev;
  1139. port->plat = pdev->dev.platform_data;
  1140. netif_napi_add(dev, &port->napi, hss_hdlc_poll, NAPI_WEIGHT);
  1141. if ((err = register_hdlc_device(dev)))
  1142. goto err_free_netdev;
  1143. platform_set_drvdata(pdev, port);
  1144. netdev_info(dev, "initialized\n");
  1145. return 0;
  1146. err_free_netdev:
  1147. free_netdev(dev);
  1148. err_plat:
  1149. npe_release(port->npe);
  1150. err_free:
  1151. kfree(port);
  1152. return err;
  1153. }
  1154. static int hss_remove_one(struct platform_device *pdev)
  1155. {
  1156. struct port *port = platform_get_drvdata(pdev);
  1157. unregister_hdlc_device(port->netdev);
  1158. free_netdev(port->netdev);
  1159. npe_release(port->npe);
  1160. kfree(port);
  1161. return 0;
  1162. }
  1163. static struct platform_driver ixp4xx_hss_driver = {
  1164. .driver.name = DRV_NAME,
  1165. .probe = hss_init_one,
  1166. .remove = hss_remove_one,
  1167. };
  1168. static int __init hss_init_module(void)
  1169. {
  1170. if ((ixp4xx_read_feature_bits() &
  1171. (IXP4XX_FEATURE_HDLC | IXP4XX_FEATURE_HSS)) !=
  1172. (IXP4XX_FEATURE_HDLC | IXP4XX_FEATURE_HSS))
  1173. return -ENODEV;
  1174. spin_lock_init(&npe_lock);
  1175. return platform_driver_register(&ixp4xx_hss_driver);
  1176. }
  1177. static void __exit hss_cleanup_module(void)
  1178. {
  1179. platform_driver_unregister(&ixp4xx_hss_driver);
  1180. }
  1181. MODULE_AUTHOR("Krzysztof Halasa");
  1182. MODULE_DESCRIPTION("Intel IXP4xx HSS driver");
  1183. MODULE_LICENSE("GPL v2");
  1184. MODULE_ALIAS("platform:ixp4xx_hss");
  1185. module_init(hss_init_module);
  1186. module_exit(hss_cleanup_module);