Home | History | Annotate | Line # | Download | only in ic
      1 /*	$NetBSD: isp1763ptd.c,v 1.1 2026/10/11 16:09:06 rkujawa Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2026 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Radoslaw Kujawa.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * ISP1763A transfer descriptors.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: isp1763ptd.c,v 1.1 2026/10/11 16:09:06 rkujawa Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/endian.h>
     41 
     42 #include <dev/ic/isp1763var.h>
     43 
     44 #define	ISP1763_CERR_MAX	3
     45 #define	ISP1763_USA_EVERY	0xff
     46 #define	ISP1763_USA_EVERY2	0x55
     47 #define	ISP1763_USA_EVERY4	0x11
     48 #define	ISP1763_USA_ONCE	0x01
     49 #define	ISP1763_HS_IVAL_MS	4
     50 #define	ISP1763_IVAL_MAX	9
     51 
     52 void
     53 isp1763hc_ptd_build(const struct isp1763hc_ptdreq *pr, uint32_t *dw)
     54 {
     55 
     56 	dw[0] = ISP1763_PTD_DW0_V |
     57 	    __SHIFTIN(pr->pr_len, ISP1763_PTD_DW0_NBYTES) |
     58 	    __SHIFTIN(pr->pr_mps, ISP1763_PTD_DW0_MPS) |
     59 	    __SHIFTIN(pr->pr_mult, ISP1763_PTD_DW0_MULT) |
     60 	    __SHIFTIN(pr->pr_ep & 1, ISP1763_PTD_DW0_EP0);
     61 
     62 	dw[1] = __SHIFTIN(pr->pr_ep >> 1, ISP1763_PTD_DW1_EP31) |
     63 	    __SHIFTIN(pr->pr_addr, ISP1763_PTD_DW1_ADDR) |
     64 	    __SHIFTIN(pr->pr_token, ISP1763_PTD_DW1_TOKEN) |
     65 	    __SHIFTIN(pr->pr_eptype, ISP1763_PTD_DW1_EPTYPE);
     66 	if (pr->pr_split) {
     67 		dw[1] |= ISP1763_PTD_DW1_SPLIT |
     68 		    __SHIFTIN(pr->pr_se, ISP1763_PTD_DW1_SE) |
     69 		    __SHIFTIN(pr->pr_port, ISP1763_PTD_DW1_PORT) |
     70 		    __SHIFTIN(pr->pr_hub, ISP1763_PTD_DW1_HUB);
     71 	}
     72 
     73 	dw[2] = __SHIFTIN(pr->pr_uframe, ISP1763_PTD_DW2_UFRAME) |
     74 	    __SHIFTIN(ISP1763_PTD_ADDR(pr->pr_mem), ISP1763_PTD_DW2_DATA);
     75 
     76 	dw[3] = ISP1763_PTD_DW3_A |
     77 	    __SHIFTIN(ISP1763_CERR_MAX, ISP1763_PTD_DW3_CERR);
     78 	if (pr->pr_toggle)
     79 		dw[3] |= ISP1763_PTD_DW3_DT;
     80 	if (pr->pr_ping)
     81 		dw[3] |= ISP1763_PTD_DW3_PING;
     82 
     83 	dw[4] = __SHIFTIN(pr->pr_usa, ISP1763_PTD_DW4_USA);
     84 	dw[5] = __SHIFTIN(pr->pr_uscs, ISP1763_PTD_DW5_USCS);
     85 	dw[6] = 0;
     86 	dw[7] = 0;
     87 }
     88 
     89 void
     90 isp1763hc_ptd_encode(const uint32_t *dw, uint8_t *buf, int ndw)
     91 {
     92 	int i;
     93 
     94 	for (i = 0; i < ndw; i++)
     95 		le32enc(buf + 4 * i, dw[i]);
     96 }
     97 
     98 void
     99 isp1763hc_ptd_decode(const uint8_t *buf, uint32_t *dw, int ndw)
    100 {
    101 	int i;
    102 
    103 	for (i = 0; i < ndw; i++)
    104 		dw[i] = le32dec(buf + 4 * i);
    105 }
    106 
    107 void
    108 isp1763hc_intr_sched(bool hs, u_int ival, uint8_t *uframe, uint8_t *usa)
    109 {
    110 	u_int b;
    111 
    112 	if (hs) {
    113 		b = MIN(MAX(ival, 1), ISP1763_IVAL_MAX);
    114 	} else {
    115 		for (b = ISP1763_HS_IVAL_MS; b < ISP1763_IVAL_MAX &&
    116 		    (2U << (b - ISP1763_HS_IVAL_MS)) <= ival; b++)
    117 			continue;
    118 	}
    119 
    120 	switch (b) {
    121 	case 1:
    122 		*usa = ISP1763_USA_EVERY;
    123 		break;
    124 	case 2:
    125 		*usa = ISP1763_USA_EVERY2;
    126 		break;
    127 	case 3:
    128 		*usa = ISP1763_USA_EVERY4;
    129 		break;
    130 	default:
    131 		*usa = ISP1763_USA_ONCE;
    132 		break;
    133 	}
    134 
    135 	if (b <= ISP1763_HS_IVAL_MS)
    136 		*uframe = 0;
    137 	else
    138 		*uframe = (1U << (b - ISP1763_HS_IVAL_MS - 1)) << 3;
    139 }
    140 
    141 uint16_t
    142 isp1763hc_last_map(uint16_t busy)
    143 {
    144 	uint16_t last;
    145 
    146 	last = 1;
    147 	while ((busy >>= 1) != 0)
    148 		last <<= 1;
    149 
    150 	return last;
    151 }
    152 
    153