python_code
stringlengths
0
1.8M
repo_name
stringclasses
7 values
file_path
stringlengths
5
99
// SPDX-License-Identifier: GPL-2.0-only /* IEEE754 floating point arithmetic * single precision */ /* * MIPS floating point support * Copyright (C) 1994-2000 Algorithmics Ltd. */ #include <linux/compiler.h> #include "ieee754sp.h" int ieee754sp_class(union ieee754sp x) { COMPXSP; EXPLODEXSP; return xc; } static inline int ieee754sp_isnan(union ieee754sp x) { return ieee754_class_nan(ieee754sp_class(x)); } static inline int ieee754sp_issnan(union ieee754sp x) { int qbit; assert(ieee754sp_isnan(x)); qbit = (SPMANT(x) & SP_MBIT(SP_FBITS - 1)) == SP_MBIT(SP_FBITS - 1); return ieee754_csr.nan2008 ^ qbit; } /* * Raise the Invalid Operation IEEE 754 exception * and convert the signaling NaN supplied to a quiet NaN. */ union ieee754sp __cold ieee754sp_nanxcpt(union ieee754sp r) { assert(ieee754sp_issnan(r)); ieee754_setcx(IEEE754_INVALID_OPERATION); if (ieee754_csr.nan2008) { SPMANT(r) |= SP_MBIT(SP_FBITS - 1); } else { SPMANT(r) &= ~SP_MBIT(SP_FBITS - 1); if (!ieee754sp_isnan(r)) SPMANT(r) |= SP_MBIT(SP_FBITS - 2); } return r; } static unsigned int ieee754sp_get_rounding(int sn, unsigned int xm) { /* inexact must round of 3 bits */ if (xm & (SP_MBIT(3) - 1)) { switch (ieee754_csr.rm) { case FPU_CSR_RZ: break; case FPU_CSR_RN: xm += 0x3 + ((xm >> 3) & 1); /* xm += (xm&0x8)?0x4:0x3 */ break; case FPU_CSR_RU: /* toward +Infinity */ if (!sn) /* ?? */ xm += 0x8; break; case FPU_CSR_RD: /* toward -Infinity */ if (sn) /* ?? */ xm += 0x8; break; } } return xm; } /* generate a normal/denormal number with over,under handling * sn is sign * xe is an unbiased exponent * xm is 3bit extended precision value. */ union ieee754sp ieee754sp_format(int sn, int xe, unsigned int xm) { assert(xm); /* we don't gen exact zeros (probably should) */ assert((xm >> (SP_FBITS + 1 + 3)) == 0); /* no excess */ assert(xm & (SP_HIDDEN_BIT << 3)); if (xe < SP_EMIN) { /* strip lower bits */ int es = SP_EMIN - xe; if (ieee754_csr.nod) { ieee754_setcx(IEEE754_UNDERFLOW); ieee754_setcx(IEEE754_INEXACT); switch(ieee754_csr.rm) { case FPU_CSR_RN: case FPU_CSR_RZ: return ieee754sp_zero(sn); case FPU_CSR_RU: /* toward +Infinity */ if (sn == 0) return ieee754sp_min(0); else return ieee754sp_zero(1); case FPU_CSR_RD: /* toward -Infinity */ if (sn == 0) return ieee754sp_zero(0); else return ieee754sp_min(1); } } if (xe == SP_EMIN - 1 && ieee754sp_get_rounding(sn, xm) >> (SP_FBITS + 1 + 3)) { /* Not tiny after rounding */ ieee754_setcx(IEEE754_INEXACT); xm = ieee754sp_get_rounding(sn, xm); xm >>= 1; /* Clear grs bits */ xm &= ~(SP_MBIT(3) - 1); xe++; } else { /* sticky right shift es bits */ xm = XSPSRS(xm, es); xe += es; assert((xm & (SP_HIDDEN_BIT << 3)) == 0); assert(xe == SP_EMIN); } } if (xm & (SP_MBIT(3) - 1)) { ieee754_setcx(IEEE754_INEXACT); if ((xm & (SP_HIDDEN_BIT << 3)) == 0) { ieee754_setcx(IEEE754_UNDERFLOW); } /* inexact must round of 3 bits */ xm = ieee754sp_get_rounding(sn, xm); /* adjust exponent for rounding add overflowing */ if (xm >> (SP_FBITS + 1 + 3)) { /* add causes mantissa overflow */ xm >>= 1; xe++; } } /* strip grs bits */ xm >>= 3; assert((xm >> (SP_FBITS + 1)) == 0); /* no excess */ assert(xe >= SP_EMIN); if (xe > SP_EMAX) { ieee754_setcx(IEEE754_OVERFLOW); ieee754_setcx(IEEE754_INEXACT); /* -O can be table indexed by (rm,sn) */ switch (ieee754_csr.rm) { case FPU_CSR_RN: return ieee754sp_inf(sn); case FPU_CSR_RZ: return ieee754sp_max(sn); case FPU_CSR_RU: /* toward +Infinity */ if (sn == 0) return ieee754sp_inf(0); else return ieee754sp_max(1); case FPU_CSR_RD: /* toward -Infinity */ if (sn == 0) return ieee754sp_max(0); else return ieee754sp_inf(1); } } /* gen norm/denorm/zero */ if ((xm & SP_HIDDEN_BIT) == 0) { /* we underflow (tiny/zero) */ assert(xe == SP_EMIN); if (ieee754_csr.mx & IEEE754_UNDERFLOW) ieee754_setcx(IEEE754_UNDERFLOW); return buildsp(sn, SP_EMIN - 1 + SP_EBIAS, xm); } else { assert((xm >> (SP_FBITS + 1)) == 0); /* no excess */ assert(xm & SP_HIDDEN_BIT); return buildsp(sn, xe + SP_EBIAS, xm & ~SP_HIDDEN_BIT); } }
linux-master
arch/mips/math-emu/ieee754sp.c
// SPDX-License-Identifier: GPL-2.0-only /* IEEE754 floating point arithmetic * double precision: common utilities */ /* * MIPS floating point support * Copyright (C) 1994-2000 Algorithmics Ltd. */ #include "ieee754dp.h" union ieee754dp ieee754dp_sub(union ieee754dp x, union ieee754dp y) { int s; COMPXDP; COMPYDP; EXPLODEXDP; EXPLODEYDP; ieee754_clearcx(); FLUSHXDP; FLUSHYDP; switch (CLPAIR(xc, yc)) { case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN): return ieee754dp_nanxcpt(y); case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF): return ieee754dp_nanxcpt(x); case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN): return y; case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF): return x; /* * Infinity handling */ case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF): if (xs != ys) return x; ieee754_setcx(IEEE754_INVALID_OPERATION); return ieee754dp_indef(); case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF): return ieee754dp_inf(ys ^ 1); case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM): return x; /* * Zero handling */ case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO): if (xs != ys) return x; else return ieee754dp_zero(ieee754_csr.rm == FPU_CSR_RD); case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO): return x; case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM): /* quick fix up */ DPSIGN(y) ^= 1; return y; case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM): DPDNORMX; fallthrough; case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM): /* normalize ym,ye */ DPDNORMY; break; case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM): /* normalize xm,xe */ DPDNORMX; break; case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM): break; } /* flip sign of y and handle as add */ ys ^= 1; assert(xm & DP_HIDDEN_BIT); assert(ym & DP_HIDDEN_BIT); /* provide guard,round and stick bit dpace */ xm <<= 3; ym <<= 3; if (xe > ye) { /* * Have to shift y fraction right to align */ s = xe - ye; ym = XDPSRS(ym, s); ye += s; } else if (ye > xe) { /* * Have to shift x fraction right to align */ s = ye - xe; xm = XDPSRS(xm, s); xe += s; } assert(xe == ye); assert(xe <= DP_EMAX); if (xs == ys) { /* generate 28 bit result of adding two 27 bit numbers */ xm = xm + ym; if (xm >> (DP_FBITS + 1 + 3)) { /* carry out */ xm = XDPSRS1(xm); /* shift preserving sticky */ xe++; } } else { if (xm >= ym) { xm = xm - ym; } else { xm = ym - xm; xs = ys; } if (xm == 0) { if (ieee754_csr.rm == FPU_CSR_RD) return ieee754dp_zero(1); /* round negative inf. => sign = -1 */ else return ieee754dp_zero(0); /* other round modes => sign = 1 */ } /* normalize to rounding precision */ while ((xm >> (DP_FBITS + 3)) == 0) { xm <<= 1; xe--; } } return ieee754dp_format(xs, xe, xm); }
linux-master
arch/mips/math-emu/dp_sub.c
// SPDX-License-Identifier: GPL-2.0-only /* IEEE754 floating point arithmetic * single precision */ /* * MIPS floating point support * Copyright (C) 1994-2000 Algorithmics Ltd. */ #include "ieee754sp.h" union ieee754sp ieee754sp_div(union ieee754sp x, union ieee754sp y) { unsigned int rm; int re; unsigned int bm; COMPXSP; COMPYSP; EXPLODEXSP; EXPLODEYSP; ieee754_clearcx(); FLUSHXSP; FLUSHYSP; switch (CLPAIR(xc, yc)) { case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN): return ieee754sp_nanxcpt(y); case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF): return ieee754sp_nanxcpt(x); case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN): return y; case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF): return x; /* * Infinity handling */ case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF): ieee754_setcx(IEEE754_INVALID_OPERATION); return ieee754sp_indef(); case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF): return ieee754sp_zero(xs ^ ys); case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM): return ieee754sp_inf(xs ^ ys); /* * Zero handling */ case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO): ieee754_setcx(IEEE754_INVALID_OPERATION); return ieee754sp_indef(); case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO): ieee754_setcx(IEEE754_ZERO_DIVIDE); return ieee754sp_inf(xs ^ ys); case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM): return ieee754sp_zero(xs == ys ? 0 : 1); case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM): SPDNORMX; fallthrough; case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM): SPDNORMY; break; case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM): SPDNORMX; break; case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM): break; } assert(xm & SP_HIDDEN_BIT); assert(ym & SP_HIDDEN_BIT); /* provide rounding space */ xm <<= 3; ym <<= 3; /* now the dirty work */ rm = 0; re = xe - ye; for (bm = SP_MBIT(SP_FBITS + 2); bm; bm >>= 1) { if (xm >= ym) { xm -= ym; rm |= bm; if (xm == 0) break; } xm <<= 1; } rm <<= 1; if (xm) rm |= 1; /* have remainder, set sticky */ assert(rm); /* normalise rm to rounding precision ? */ while ((rm >> (SP_FBITS + 3)) == 0) { rm <<= 1; re--; } return ieee754sp_format(xs == ys ? 0 : 1, re, rm); }
linux-master
arch/mips/math-emu/sp_div.c
// SPDX-License-Identifier: GPL-2.0-only /* * IEEE754 floating point arithmetic * double precision: MIN{,A}.f * MIN : Scalar Floating-Point Minimum * MINA: Scalar Floating-Point argument with Minimum Absolute Value * * MIN.D : FPR[fd] = minNum(FPR[fs],FPR[ft]) * MINA.D: FPR[fd] = maxNumMag(FPR[fs],FPR[ft]) * * MIPS floating point support * Copyright (C) 2015 Imagination Technologies, Ltd. * Author: Markos Chandras <[email protected]> */ #include "ieee754dp.h" union ieee754dp ieee754dp_fmin(union ieee754dp x, union ieee754dp y) { COMPXDP; COMPYDP; EXPLODEXDP; EXPLODEYDP; FLUSHXDP; FLUSHYDP; ieee754_clearcx(); switch (CLPAIR(xc, yc)) { case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN): return ieee754dp_nanxcpt(y); case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF): return ieee754dp_nanxcpt(x); /* * Quiet NaN handling */ /* * The case of both inputs quiet NaNs */ case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN): return x; /* * The cases of exactly one input quiet NaN (numbers * are here preferred as returned values to NaNs) */ case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN): return x; case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF): return y; /* * Infinity and zero handling */ case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO): return xs ? x : y; case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM): return ys ? y : x; case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO): return ieee754dp_zero(xs | ys); case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM): DPDNORMX; fallthrough; case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM): DPDNORMY; break; case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM): DPDNORMX; } /* Finally get to do some computation */ assert(xm & DP_HIDDEN_BIT); assert(ym & DP_HIDDEN_BIT); /* Compare signs */ if (xs > ys) return x; else if (xs < ys) return y; /* Signs of inputs are the same, let's compare exponents */ if (xs == 0) { /* Inputs are both positive */ if (xe > ye) return y; else if (xe < ye) return x; } else { /* Inputs are both negative */ if (xe > ye) return x; else if (xe < ye) return y; } /* Signs and exponents of inputs are equal, let's compare mantissas */ if (xs == 0) { /* Inputs are both positive, with equal signs and exponents */ if (xm <= ym) return x; return y; } /* Inputs are both negative, with equal signs and exponents */ if (xm <= ym) return y; return x; } union ieee754dp ieee754dp_fmina(union ieee754dp x, union ieee754dp y) { COMPXDP; COMPYDP; EXPLODEXDP; EXPLODEYDP; FLUSHXDP; FLUSHYDP; ieee754_clearcx(); switch (CLPAIR(xc, yc)) { case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN): return ieee754dp_nanxcpt(y); case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF): return ieee754dp_nanxcpt(x); /* * Quiet NaN handling */ /* * The case of both inputs quiet NaNs */ case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN): return x; /* * The cases of exactly one input quiet NaN (numbers * are here preferred as returned values to NaNs) */ case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN): return x; case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF): return y; /* * Infinity and zero handling */ case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF): return ieee754dp_inf(xs | ys); case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO): return y; case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM): return x; case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO): return ieee754dp_zero(xs | ys); case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM): DPDNORMX; fallthrough; case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM): DPDNORMY; break; case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM): DPDNORMX; } /* Finally get to do some computation */ assert(xm & DP_HIDDEN_BIT); assert(ym & DP_HIDDEN_BIT); /* Compare exponent */ if (xe > ye) return y; else if (xe < ye) return x; /* Compare mantissa */ if (xm < ym) return x; else if (xm > ym) return y; else if (xs == 1) return x; return y; }
linux-master
arch/mips/math-emu/dp_fmin.c
// SPDX-License-Identifier: GPL-2.0-only /* IEEE754 floating point arithmetic * single precision */ /* * MIPS floating point support * Copyright (C) 1994-2000 Algorithmics Ltd. */ #include "ieee754sp.h" union ieee754sp ieee754sp_fint(int x) { unsigned int xm; int xe; int xs; ieee754_clearcx(); if (x == 0) return ieee754sp_zero(0); if (x == 1 || x == -1) return ieee754sp_one(x < 0); if (x == 10 || x == -10) return ieee754sp_ten(x < 0); xs = (x < 0); if (xs) { if (x == (1 << 31)) xm = ((unsigned) 1 << 31); /* max neg can't be safely negated */ else xm = -x; } else { xm = x; } xe = SP_FBITS + 3; if (xm >> (SP_FBITS + 1 + 3)) { /* shunt out overflow bits */ while (xm >> (SP_FBITS + 1 + 3)) { SPXSRSX1(); } } else { /* normalize in grs extended single precision */ while ((xm >> (SP_FBITS + 3)) == 0) { xm <<= 1; xe--; } } return ieee754sp_format(xs, xe, xm); }
linux-master
arch/mips/math-emu/sp_fint.c
// SPDX-License-Identifier: GPL-2.0-only /* IEEE754 floating point arithmetic * double precision: common utilities */ /* * MIPS floating point support * Copyright (C) 1994-2000 Algorithmics Ltd. */ #include "ieee754dp.h" int ieee754dp_tint(union ieee754dp x) { u64 residue; int round; int sticky; int odd; COMPXDP; ieee754_clearcx(); EXPLODEXDP; FLUSHXDP; switch (xc) { case IEEE754_CLASS_SNAN: case IEEE754_CLASS_QNAN: ieee754_setcx(IEEE754_INVALID_OPERATION); return ieee754si_indef(); case IEEE754_CLASS_INF: ieee754_setcx(IEEE754_INVALID_OPERATION); return ieee754si_overflow(xs); case IEEE754_CLASS_ZERO: return 0; case IEEE754_CLASS_DNORM: case IEEE754_CLASS_NORM: break; } if (xe > 31) { /* Set invalid. We will only use overflow for floating point overflow */ ieee754_setcx(IEEE754_INVALID_OPERATION); return ieee754si_overflow(xs); } /* oh gawd */ if (xe > DP_FBITS) { xm <<= xe - DP_FBITS; } else if (xe < DP_FBITS) { if (xe < -1) { residue = xm; round = 0; sticky = residue != 0; xm = 0; } else { residue = xm << (64 - DP_FBITS + xe); round = (residue >> 63) != 0; sticky = (residue << 1) != 0; xm >>= DP_FBITS - xe; } /* Note: At this point upper 32 bits of xm are guaranteed to be zero */ odd = (xm & 0x1) != 0x0; switch (ieee754_csr.rm) { case FPU_CSR_RN: if (round && (sticky || odd)) xm++; break; case FPU_CSR_RZ: break; case FPU_CSR_RU: /* toward +Infinity */ if ((round || sticky) && !xs) xm++; break; case FPU_CSR_RD: /* toward -Infinity */ if ((round || sticky) && xs) xm++; break; } /* look for valid corner case 0x80000000 */ if ((xm >> 31) != 0 && (xs == 0 || xm != 0x80000000)) { /* This can happen after rounding */ ieee754_setcx(IEEE754_INVALID_OPERATION); return ieee754si_overflow(xs); } if (round || sticky) ieee754_setcx(IEEE754_INEXACT); } if (xs) return -xm; else return xm; }
linux-master
arch/mips/math-emu/dp_tint.c
// SPDX-License-Identifier: GPL-2.0-only /* IEEE754 floating point arithmetic * double precision: common utilities */ /* * MIPS floating point support * Copyright (C) 1994-2000 Algorithmics Ltd. */ #include "ieee754dp.h" int ieee754dp_cmp(union ieee754dp x, union ieee754dp y, int cmp, int sig) { s64 vx; s64 vy; COMPXDP; COMPYDP; EXPLODEXDP; EXPLODEYDP; FLUSHXDP; FLUSHYDP; ieee754_clearcx(); /* Even clear inexact flag here */ if (ieee754_class_nan(xc) || ieee754_class_nan(yc)) { if (sig || xc == IEEE754_CLASS_SNAN || yc == IEEE754_CLASS_SNAN) ieee754_setcx(IEEE754_INVALID_OPERATION); return (cmp & IEEE754_CUN) != 0; } else { vx = x.bits; vy = y.bits; if (vx < 0) vx = -vx ^ DP_SIGN_BIT; if (vy < 0) vy = -vy ^ DP_SIGN_BIT; if (vx < vy) return (cmp & IEEE754_CLT) != 0; else if (vx == vy) return (cmp & IEEE754_CEQ) != 0; else return (cmp & IEEE754_CGT) != 0; } }
linux-master
arch/mips/math-emu/dp_cmp.c
// SPDX-License-Identifier: GPL-2.0-only /* IEEE754 floating point arithmetic * double precision: common utilities */ /* * MIPS floating point support * Copyright (C) 1994-2000 Algorithmics Ltd. */ #include "ieee754dp.h" union ieee754dp ieee754dp_add(union ieee754dp x, union ieee754dp y) { int s; COMPXDP; COMPYDP; EXPLODEXDP; EXPLODEYDP; ieee754_clearcx(); FLUSHXDP; FLUSHYDP; switch (CLPAIR(xc, yc)) { case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN): return ieee754dp_nanxcpt(y); case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM): case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF): return ieee754dp_nanxcpt(x); case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN): return y; case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM): case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF): return x; /* * Infinity handling */ case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF): if (xs == ys) return x; ieee754_setcx(IEEE754_INVALID_OPERATION); return ieee754dp_indef(); case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF): return y; case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM): return x; /* * Zero handling */ case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO): if (xs == ys) return x; else return ieee754dp_zero(ieee754_csr.rm == FPU_CSR_RD); case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO): case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO): return x; case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM): case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM): return y; case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM): DPDNORMX; fallthrough; case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM): DPDNORMY; break; case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM): DPDNORMX; break; case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM): break; } assert(xm & DP_HIDDEN_BIT); assert(ym & DP_HIDDEN_BIT); /* * Provide guard,round and stick bit space. */ xm <<= 3; ym <<= 3; if (xe > ye) { /* * Have to shift y fraction right to align. */ s = xe - ye; ym = XDPSRS(ym, s); ye += s; } else if (ye > xe) { /* * Have to shift x fraction right to align. */ s = ye - xe; xm = XDPSRS(xm, s); xe += s; } assert(xe == ye); assert(xe <= DP_EMAX); if (xs == ys) { /* * Generate 28 bit result of adding two 27 bit numbers * leaving result in xm, xs and xe. */ xm = xm + ym; if (xm >> (DP_FBITS + 1 + 3)) { /* carry out */ xm = XDPSRS1(xm); xe++; } } else { if (xm >= ym) { xm = xm - ym; } else { xm = ym - xm; xs = ys; } if (xm == 0) return ieee754dp_zero(ieee754_csr.rm == FPU_CSR_RD); /* * Normalize to rounding precision. */ while ((xm >> (DP_FBITS + 3)) == 0) { xm <<= 1; xe--; } } return ieee754dp_format(xs, xe, xm); }
linux-master
arch/mips/math-emu/dp_add.c
// SPDX-License-Identifier: GPL-2.0-only #include <byteswap.h> #include <elf.h> #include <endian.h> #include <errno.h> #include <fcntl.h> #include <inttypes.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #ifdef be32toh /* If libc provides le{16,32,64}toh() then we'll use them */ #elif BYTE_ORDER == LITTLE_ENDIAN # define le16toh(x) (x) # define le32toh(x) (x) # define le64toh(x) (x) #elif BYTE_ORDER == BIG_ENDIAN # define le16toh(x) bswap_16(x) # define le32toh(x) bswap_32(x) # define le64toh(x) bswap_64(x) #endif /* MIPS opcodes, in bits 31:26 of an instruction */ #define OP_SPECIAL 0x00 #define OP_REGIMM 0x01 #define OP_BEQ 0x04 #define OP_BNE 0x05 #define OP_BLEZ 0x06 #define OP_BGTZ 0x07 #define OP_BEQL 0x14 #define OP_BNEL 0x15 #define OP_BLEZL 0x16 #define OP_BGTZL 0x17 #define OP_LL 0x30 #define OP_LLD 0x34 #define OP_SC 0x38 #define OP_SCD 0x3c /* Bits 20:16 of OP_REGIMM instructions */ #define REGIMM_BLTZ 0x00 #define REGIMM_BGEZ 0x01 #define REGIMM_BLTZL 0x02 #define REGIMM_BGEZL 0x03 #define REGIMM_BLTZAL 0x10 #define REGIMM_BGEZAL 0x11 #define REGIMM_BLTZALL 0x12 #define REGIMM_BGEZALL 0x13 /* Bits 5:0 of OP_SPECIAL instructions */ #define SPECIAL_SYNC 0x0f static void usage(FILE *f) { fprintf(f, "Usage: loongson3-llsc-check /path/to/vmlinux\n"); } static int se16(uint16_t x) { return (int16_t)x; } static bool is_ll(uint32_t insn) { switch (insn >> 26) { case OP_LL: case OP_LLD: return true; default: return false; } } static bool is_sc(uint32_t insn) { switch (insn >> 26) { case OP_SC: case OP_SCD: return true; default: return false; } } static bool is_sync(uint32_t insn) { /* Bits 31:11 should all be zeroes */ if (insn >> 11) return false; /* Bits 5:0 specify the SYNC special encoding */ if ((insn & 0x3f) != SPECIAL_SYNC) return false; return true; } static bool is_branch(uint32_t insn, int *off) { switch (insn >> 26) { case OP_BEQ: case OP_BEQL: case OP_BNE: case OP_BNEL: case OP_BGTZ: case OP_BGTZL: case OP_BLEZ: case OP_BLEZL: *off = se16(insn) + 1; return true; case OP_REGIMM: switch ((insn >> 16) & 0x1f) { case REGIMM_BGEZ: case REGIMM_BGEZL: case REGIMM_BGEZAL: case REGIMM_BGEZALL: case REGIMM_BLTZ: case REGIMM_BLTZL: case REGIMM_BLTZAL: case REGIMM_BLTZALL: *off = se16(insn) + 1; return true; default: return false; } default: return false; } } static int check_ll(uint64_t pc, uint32_t *code, size_t sz) { ssize_t i, max, sc_pos; int off; /* * Every LL must be preceded by a sync instruction in order to ensure * that instruction reordering doesn't allow a prior memory access to * execute after the LL & cause erroneous results. */ if (!is_sync(le32toh(code[-1]))) { fprintf(stderr, "%" PRIx64 ": LL not preceded by sync\n", pc); return -EINVAL; } /* Find the matching SC instruction */ max = sz / 4; for (sc_pos = 0; sc_pos < max; sc_pos++) { if (is_sc(le32toh(code[sc_pos]))) break; } if (sc_pos >= max) { fprintf(stderr, "%" PRIx64 ": LL has no matching SC\n", pc); return -EINVAL; } /* * Check branches within the LL/SC loop target sync instructions, * ensuring that speculative execution can't generate memory accesses * due to instructions outside of the loop. */ for (i = 0; i < sc_pos; i++) { if (!is_branch(le32toh(code[i]), &off)) continue; /* * If the branch target is within the LL/SC loop then we don't * need to worry about it. */ if ((off >= -i) && (off <= sc_pos)) continue; /* If the branch targets a sync instruction we're all good... */ if (is_sync(le32toh(code[i + off]))) continue; /* ...but if not, we have a problem */ fprintf(stderr, "%" PRIx64 ": Branch target not a sync\n", pc + (i * 4)); return -EINVAL; } return 0; } static int check_code(uint64_t pc, uint32_t *code, size_t sz) { int err = 0; if (sz % 4) { fprintf(stderr, "%" PRIx64 ": Section size not a multiple of 4\n", pc); err = -EINVAL; sz -= (sz % 4); } if (is_ll(le32toh(code[0]))) { fprintf(stderr, "%" PRIx64 ": First instruction in section is an LL\n", pc); err = -EINVAL; } #define advance() ( \ code++, \ pc += 4, \ sz -= 4 \ ) /* * Skip the first instruction, allowing check_ll to look backwards * unconditionally. */ advance(); /* Now scan through the code looking for LL instructions */ for (; sz; advance()) { if (is_ll(le32toh(code[0]))) err |= check_ll(pc, code, sz); } return err; } int main(int argc, char *argv[]) { int vmlinux_fd, status, err, i; const char *vmlinux_path; struct stat st; Elf64_Ehdr *eh; Elf64_Shdr *sh; void *vmlinux; status = EXIT_FAILURE; if (argc < 2) { usage(stderr); goto out_ret; } vmlinux_path = argv[1]; vmlinux_fd = open(vmlinux_path, O_RDONLY); if (vmlinux_fd == -1) { perror("Unable to open vmlinux"); goto out_ret; } err = fstat(vmlinux_fd, &st); if (err) { perror("Unable to stat vmlinux"); goto out_close; } vmlinux = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, vmlinux_fd, 0); if (vmlinux == MAP_FAILED) { perror("Unable to mmap vmlinux"); goto out_close; } eh = vmlinux; if (memcmp(eh->e_ident, ELFMAG, SELFMAG)) { fprintf(stderr, "vmlinux is not an ELF?\n"); goto out_munmap; } if (eh->e_ident[EI_CLASS] != ELFCLASS64) { fprintf(stderr, "vmlinux is not 64b?\n"); goto out_munmap; } if (eh->e_ident[EI_DATA] != ELFDATA2LSB) { fprintf(stderr, "vmlinux is not little endian?\n"); goto out_munmap; } for (i = 0; i < le16toh(eh->e_shnum); i++) { sh = vmlinux + le64toh(eh->e_shoff) + (i * le16toh(eh->e_shentsize)); if (sh->sh_type != SHT_PROGBITS) continue; if (!(sh->sh_flags & SHF_EXECINSTR)) continue; err = check_code(le64toh(sh->sh_addr), vmlinux + le64toh(sh->sh_offset), le64toh(sh->sh_size)); if (err) goto out_munmap; } status = EXIT_SUCCESS; out_munmap: munmap(vmlinux, st.st_size); out_close: close(vmlinux_fd); out_ret: fprintf(stdout, "loongson3-llsc-check returns %s\n", status ? "failure" : "success"); return status; }
linux-master
arch/mips/tools/loongson3-llsc-check.c
// SPDX-License-Identifier: GPL-2.0 #include <byteswap.h> #include <elf.h> #include <endian.h> #include <inttypes.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef be32toh /* If libc provides [bl]e{32,64}toh() then we'll use them */ #elif BYTE_ORDER == LITTLE_ENDIAN # define be32toh(x) bswap_32(x) # define le32toh(x) (x) # define be64toh(x) bswap_64(x) # define le64toh(x) (x) #elif BYTE_ORDER == BIG_ENDIAN # define be32toh(x) (x) # define le32toh(x) bswap_32(x) # define be64toh(x) (x) # define le64toh(x) bswap_64(x) #endif __attribute__((noreturn)) static void die(const char *msg) { fputs(msg, stderr); exit(EXIT_FAILURE); } int main(int argc, const char *argv[]) { uint64_t entry; size_t nread; FILE *file; union { Elf32_Ehdr ehdr32; Elf64_Ehdr ehdr64; } hdr; if (argc != 2) die("Usage: elf-entry <elf-file>\n"); file = fopen(argv[1], "r"); if (!file) { perror("Unable to open input file"); return EXIT_FAILURE; } nread = fread(&hdr, 1, sizeof(hdr), file); if (nread != sizeof(hdr)) { perror("Unable to read input file"); fclose(file); return EXIT_FAILURE; } if (memcmp(hdr.ehdr32.e_ident, ELFMAG, SELFMAG)) { fclose(file); die("Input is not an ELF\n"); } switch (hdr.ehdr32.e_ident[EI_CLASS]) { case ELFCLASS32: switch (hdr.ehdr32.e_ident[EI_DATA]) { case ELFDATA2LSB: entry = le32toh(hdr.ehdr32.e_entry); break; case ELFDATA2MSB: entry = be32toh(hdr.ehdr32.e_entry); break; default: fclose(file); die("Invalid ELF encoding\n"); } /* Sign extend to form a canonical address */ entry = (int64_t)(int32_t)entry; break; case ELFCLASS64: switch (hdr.ehdr32.e_ident[EI_DATA]) { case ELFDATA2LSB: entry = le64toh(hdr.ehdr64.e_entry); break; case ELFDATA2MSB: entry = be64toh(hdr.ehdr64.e_entry); break; default: fclose(file); die("Invalid ELF encoding\n"); } break; default: fclose(file); die("Invalid ELF class\n"); } printf("0x%016" PRIx64 "\n", entry); fclose(file); return EXIT_SUCCESS; }
linux-master
arch/mips/tools/elf-entry.c
// SPDX-License-Identifier: GPL-2.0 /* * ip22-mc.c: Routines for manipulating SGI Memory Controller. * * Copyright (C) 1996 David S. Miller ([email protected]) * Copyright (C) 1999 Andrew R. Baker ([email protected]) - Indigo2 changes * Copyright (C) 2003 Ladislav Michl ([email protected]) * Copyright (C) 2004 Peter Fuerst ([email protected]) - IP28 */ #include <linux/init.h> #include <linux/export.h> #include <linux/kernel.h> #include <linux/memblock.h> #include <linux/spinlock.h> #include <asm/io.h> #include <asm/bootinfo.h> #include <asm/sgialib.h> #include <asm/sgi/mc.h> #include <asm/sgi/hpc3.h> #include <asm/sgi/ip22.h> struct sgimc_regs *sgimc; EXPORT_SYMBOL(sgimc); static inline unsigned long get_bank_addr(unsigned int memconfig) { return (memconfig & SGIMC_MCONFIG_BASEADDR) << ((sgimc->systemid & SGIMC_SYSID_MASKREV) >= 5 ? 24 : 22); } static inline unsigned long get_bank_size(unsigned int memconfig) { return ((memconfig & SGIMC_MCONFIG_RMASK) + 0x0100) << ((sgimc->systemid & SGIMC_SYSID_MASKREV) >= 5 ? 16 : 14); } static inline unsigned int get_bank_config(int bank) { unsigned int res = bank > 1 ? sgimc->mconfig1 : sgimc->mconfig0; return bank % 2 ? res & 0xffff : res >> 16; } #if defined(CONFIG_SGI_IP28) || defined(CONFIG_32BIT) static void __init probe_memory(void) { /* prom detects all usable memory */ } #else /* * Detect installed memory, which PROM misses */ static void __init probe_memory(void) { unsigned long addr, size; int i; printk(KERN_INFO "MC: Probing memory configuration:\n"); for (i = 0; i < 4; i++) { unsigned int tmp = get_bank_config(i); if (!(tmp & SGIMC_MCONFIG_BVALID)) continue; size = get_bank_size(tmp); addr = get_bank_addr(tmp); printk(KERN_INFO " bank%d: %3ldM @ %08lx\n", i, size / 1024 / 1024, addr); if (addr >= SGIMC_SEG1_BADDR) memblock_add(addr, size); } } #endif void __init sgimc_init(void) { u32 tmp; /* ioremap can't fail */ sgimc = (struct sgimc_regs *) ioremap(SGIMC_BASE, sizeof(struct sgimc_regs)); printk(KERN_INFO "MC: SGI memory controller Revision %d\n", (int) sgimc->systemid & SGIMC_SYSID_MASKREV); /* Place the MC into a known state. This must be done before * interrupts are first enabled etc. */ /* Step 0: Make sure we turn off the watchdog in case it's * still running (which might be the case after a * soft reboot). */ tmp = sgimc->cpuctrl0; tmp &= ~SGIMC_CCTRL0_WDOG; sgimc->cpuctrl0 = tmp; /* Step 1: The CPU/GIO error status registers will not latch * up a new error status until the register has been * cleared by the cpu. These status registers are * cleared by writing any value to them. */ sgimc->cstat = sgimc->gstat = 0; /* Step 2: Enable all parity checking in cpu control register * zero. */ /* don't touch parity settings for IP28 */ tmp = sgimc->cpuctrl0; #ifndef CONFIG_SGI_IP28 tmp |= SGIMC_CCTRL0_EPERRGIO | SGIMC_CCTRL0_EPERRMEM; #endif tmp |= SGIMC_CCTRL0_R4KNOCHKPARR; sgimc->cpuctrl0 = tmp; /* Step 3: Setup the MC write buffer depth, this is controlled * in cpu control register 1 in the lower 4 bits. */ tmp = sgimc->cpuctrl1; tmp &= ~0xf; tmp |= 0xd; sgimc->cpuctrl1 = tmp; /* Step 4: Initialize the RPSS divider register to run as fast * as it can correctly operate. The register is laid * out as follows: * * ---------------------------------------- * | RESERVED | INCREMENT | DIVIDER | * ---------------------------------------- * 31 16 15 8 7 0 * * DIVIDER determines how often a 'tick' happens, * INCREMENT determines by how the RPSS increment * registers value increases at each 'tick'. Thus, * for IP22 we get INCREMENT=1, DIVIDER=1 == 0x101 */ sgimc->divider = 0x101; /* Step 5: Initialize GIO64 arbitrator configuration register. * * NOTE: HPC init code in sgihpc_init() must run before us because * we need to know Guiness vs. FullHouse and the board * revision on this machine. You have been warned. */ /* First the basic invariants across all GIO64 implementations. */ tmp = sgimc->giopar & SGIMC_GIOPAR_GFX64; /* keep gfx 64bit settings */ tmp |= SGIMC_GIOPAR_HPC64; /* All 1st HPC's interface at 64bits */ tmp |= SGIMC_GIOPAR_ONEBUS; /* Only one physical GIO bus exists */ if (ip22_is_fullhouse()) { /* Fullhouse specific settings. */ if (SGIOC_SYSID_BOARDREV(sgioc->sysid) < 2) { tmp |= SGIMC_GIOPAR_HPC264; /* 2nd HPC at 64bits */ tmp |= SGIMC_GIOPAR_PLINEEXP0; /* exp0 pipelines */ tmp |= SGIMC_GIOPAR_MASTEREXP1; /* exp1 masters */ tmp |= SGIMC_GIOPAR_RTIMEEXP0; /* exp0 is realtime */ } else { tmp |= SGIMC_GIOPAR_HPC264; /* 2nd HPC 64bits */ tmp |= SGIMC_GIOPAR_PLINEEXP0; /* exp[01] pipelined */ tmp |= SGIMC_GIOPAR_PLINEEXP1; tmp |= SGIMC_GIOPAR_MASTEREISA; /* EISA masters */ } } else { /* Guiness specific settings. */ tmp |= SGIMC_GIOPAR_EISA64; /* MC talks to EISA at 64bits */ tmp |= SGIMC_GIOPAR_MASTEREISA; /* EISA bus can act as master */ } sgimc->giopar = tmp; /* poof */ probe_memory(); } #ifdef CONFIG_SGI_IP28 void __init prom_cleanup(void) { u32 mconfig1; unsigned long flags; spinlock_t lock; /* * because ARCS accesses memory uncached we wait until ARCS * isn't needed any longer, before we switch from slow to * normal mode */ spin_lock_irqsave(&lock, flags); mconfig1 = sgimc->mconfig1; /* map ECC register */ sgimc->mconfig1 = (mconfig1 & 0xffff0000) | 0x2060; iob(); /* switch to normal mode */ *(unsigned long *)PHYS_TO_XKSEG_UNCACHED(0x60000000) = 0; iob(); /* reduce WR_COL */ sgimc->cmacc = (sgimc->cmacc & ~0xf) | 4; iob(); /* restore old config */ sgimc->mconfig1 = mconfig1; iob(); spin_unlock_irqrestore(&lock, flags); } #endif
linux-master
arch/mips/sgi-ip22/ip22-mc.c
// SPDX-License-Identifier: GPL-2.0 #include <linux/init.h> #include <linux/if_ether.h> #include <linux/kernel.h> #include <linux/platform_device.h> #include <linux/dma-mapping.h> #include <asm/paccess.h> #include <asm/sgi/ip22.h> #include <asm/sgi/hpc3.h> #include <asm/sgi/mc.h> #include <asm/sgi/seeq.h> #include <asm/sgi/wd.h> static struct resource sgiwd93_0_resources[] = { { .name = "eth0 irq", .start = SGI_WD93_0_IRQ, .end = SGI_WD93_0_IRQ, .flags = IORESOURCE_IRQ } }; static struct sgiwd93_platform_data sgiwd93_0_pd = { .unit = 0, .irq = SGI_WD93_0_IRQ, }; static u64 sgiwd93_0_dma_mask = DMA_BIT_MASK(32); static struct platform_device sgiwd93_0_device = { .name = "sgiwd93", .id = 0, .num_resources = ARRAY_SIZE(sgiwd93_0_resources), .resource = sgiwd93_0_resources, .dev = { .platform_data = &sgiwd93_0_pd, .dma_mask = &sgiwd93_0_dma_mask, .coherent_dma_mask = DMA_BIT_MASK(32), }, }; static struct resource sgiwd93_1_resources[] = { { .name = "eth0 irq", .start = SGI_WD93_1_IRQ, .end = SGI_WD93_1_IRQ, .flags = IORESOURCE_IRQ } }; static struct sgiwd93_platform_data sgiwd93_1_pd = { .unit = 1, .irq = SGI_WD93_1_IRQ, }; static u64 sgiwd93_1_dma_mask = DMA_BIT_MASK(32); static struct platform_device sgiwd93_1_device = { .name = "sgiwd93", .id = 1, .num_resources = ARRAY_SIZE(sgiwd93_1_resources), .resource = sgiwd93_1_resources, .dev = { .platform_data = &sgiwd93_1_pd, .dma_mask = &sgiwd93_1_dma_mask, .coherent_dma_mask = DMA_BIT_MASK(32), }, }; /* * Create a platform device for the GPI port that receives the * image data from the embedded camera. */ static int __init sgiwd93_devinit(void) { int res; sgiwd93_0_pd.hregs = &hpc3c0->scsi_chan0; sgiwd93_0_pd.wdregs = (unsigned char *) hpc3c0->scsi0_ext; res = platform_device_register(&sgiwd93_0_device); if (res) return res; if (!ip22_is_fullhouse()) return 0; sgiwd93_1_pd.hregs = &hpc3c0->scsi_chan1; sgiwd93_1_pd.wdregs = (unsigned char *) hpc3c0->scsi1_ext; return platform_device_register(&sgiwd93_1_device); } device_initcall(sgiwd93_devinit); static struct resource sgiseeq_0_resources[] = { { .name = "eth0 irq", .start = SGI_ENET_IRQ, .end = SGI_ENET_IRQ, .flags = IORESOURCE_IRQ } }; static struct sgiseeq_platform_data eth0_pd; static u64 sgiseeq_dma_mask = DMA_BIT_MASK(32); static struct platform_device eth0_device = { .name = "sgiseeq", .id = 0, .num_resources = ARRAY_SIZE(sgiseeq_0_resources), .resource = sgiseeq_0_resources, .dev = { .platform_data = &eth0_pd, .dma_mask = &sgiseeq_dma_mask, .coherent_dma_mask = DMA_BIT_MASK(32), }, }; static struct resource sgiseeq_1_resources[] = { { .name = "eth1 irq", .start = SGI_GIO_0_IRQ, .end = SGI_GIO_0_IRQ, .flags = IORESOURCE_IRQ } }; static struct sgiseeq_platform_data eth1_pd; static struct platform_device eth1_device = { .name = "sgiseeq", .id = 1, .num_resources = ARRAY_SIZE(sgiseeq_1_resources), .resource = sgiseeq_1_resources, .dev = { .platform_data = &eth1_pd, }, }; /* * Create a platform device for the GPI port that receives the * image data from the embedded camera. */ static int __init sgiseeq_devinit(void) { unsigned int pbdma __maybe_unused; int res, i; eth0_pd.hpc = hpc3c0; eth0_pd.irq = SGI_ENET_IRQ; #define EADDR_NVOFS 250 for (i = 0; i < 3; i++) { unsigned short tmp = ip22_nvram_read(EADDR_NVOFS / 2 + i); eth0_pd.mac[2 * i] = tmp >> 8; eth0_pd.mac[2 * i + 1] = tmp & 0xff; } res = platform_device_register(&eth0_device); if (res) return res; /* Second HPC is missing? */ if (ip22_is_fullhouse() || get_dbe(pbdma, (unsigned int *)&hpc3c1->pbdma[1])) return 0; sgimc->giopar |= SGIMC_GIOPAR_MASTEREXP1 | SGIMC_GIOPAR_EXP164 | SGIMC_GIOPAR_HPC264; hpc3c1->pbus_piocfg[0][0] = 0x3ffff; /* interrupt/config register on Challenge S Mezz board */ hpc3c1->pbus_extregs[0][0] = 0x30; eth1_pd.hpc = hpc3c1; eth1_pd.irq = SGI_GIO_0_IRQ; #define EADDR_NVOFS 250 for (i = 0; i < 3; i++) { unsigned short tmp = ip22_eeprom_read(&hpc3c1->eeprom, EADDR_NVOFS / 2 + i); eth1_pd.mac[2 * i] = tmp >> 8; eth1_pd.mac[2 * i + 1] = tmp & 0xff; } return platform_device_register(&eth1_device); } device_initcall(sgiseeq_devinit); static int __init sgi_hal2_devinit(void) { return IS_ERR(platform_device_register_simple("sgihal2", 0, NULL, 0)); } device_initcall(sgi_hal2_devinit); static int __init sgi_button_devinit(void) { if (ip22_is_fullhouse()) return 0; /* full house has no volume buttons */ return IS_ERR(platform_device_register_simple("sgibtns", -1, NULL, 0)); } device_initcall(sgi_button_devinit); static int __init sgi_ds1286_devinit(void) { struct resource res; memset(&res, 0, sizeof(res)); res.start = HPC3_CHIP0_BASE + offsetof(struct hpc3_regs, rtcregs); res.end = res.start + sizeof(hpc3c0->rtcregs) - 1; res.flags = IORESOURCE_MEM; return IS_ERR(platform_device_register_simple("rtc-ds1286", -1, &res, 1)); } device_initcall(sgi_ds1286_devinit);
linux-master
arch/mips/sgi-ip22/ip22-platform.c
/* * Basic EISA bus support for the SGI Indigo-2. * * (C) 2002 Pascal Dameme <[email protected]> * and Marc Zyngier <[email protected]> * * This code is released under both the GPL version 2 and BSD * licenses. Either license may be used. * * This code offers a very basic support for this EISA bus present in * the SGI Indigo-2. It currently only supports PIO (forget about DMA * for the time being). This is enough for a low-end ethernet card, * but forget about your favorite SCSI card... * * TODO : * - Fix bugs... * - Add ISA support * - Add DMA (yeah, right...). * - Fix more bugs. */ #include <linux/eisa.h> #include <linux/types.h> #include <linux/init.h> #include <linux/irq.h> #include <linux/kernel_stat.h> #include <linux/signal.h> #include <linux/sched.h> #include <linux/interrupt.h> #include <linux/delay.h> #include <asm/io.h> #include <asm/irq.h> #include <asm/mipsregs.h> #include <asm/addrspace.h> #include <asm/processor.h> #include <asm/sgi/ioc.h> #include <asm/sgi/mc.h> #include <asm/sgi/ip22.h> #include <asm/i8259.h> /* I2 has four EISA slots. */ #define IP22_EISA_MAX_SLOTS 4 #define EISA_MAX_IRQ 16 #define EIU_MODE_REG 0x0001ffc0 #define EIU_STAT_REG 0x0001ffc4 #define EIU_PREMPT_REG 0x0001ffc8 #define EIU_QUIET_REG 0x0001ffcc #define EIU_INTRPT_ACK 0x00010004 static char __init *decode_eisa_sig(unsigned long addr) { static char sig_str[EISA_SIG_LEN] __initdata; u8 sig[4]; u16 rev; int i; for (i = 0; i < 4; i++) { sig[i] = inb(addr + i); if (!i && (sig[0] & 0x80)) return NULL; } sig_str[0] = ((sig[0] >> 2) & 0x1f) + ('A' - 1); sig_str[1] = (((sig[0] & 3) << 3) | (sig[1] >> 5)) + ('A' - 1); sig_str[2] = (sig[1] & 0x1f) + ('A' - 1); rev = (sig[2] << 8) | sig[3]; sprintf(sig_str + 3, "%04X", rev); return sig_str; } static irqreturn_t ip22_eisa_intr(int irq, void *dev_id) { u8 eisa_irq = inb(EIU_INTRPT_ACK); inb(EISA_DMA1_STATUS); inb(EISA_DMA2_STATUS); if (eisa_irq < EISA_MAX_IRQ) { do_IRQ(eisa_irq); return IRQ_HANDLED; } /* Oops, Bad Stuff Happened... */ printk(KERN_ERR "eisa_irq %d out of bound\n", eisa_irq); outb(0x20, EISA_INT2_CTRL); outb(0x20, EISA_INT1_CTRL); return IRQ_NONE; } int __init ip22_eisa_init(void) { int i, c; char *str; if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) { printk(KERN_INFO "EISA: bus not present.\n"); return 1; } printk(KERN_INFO "EISA: Probing bus...\n"); for (c = 0, i = 1; i <= IP22_EISA_MAX_SLOTS; i++) { if ((str = decode_eisa_sig(0x1000 * i + EISA_VENDOR_ID_OFFSET))) { printk(KERN_INFO "EISA: slot %d : %s detected.\n", i, str); c++; } } printk(KERN_INFO "EISA: Detected %d card%s.\n", c, c < 2 ? "" : "s"); #ifdef CONFIG_ISA printk(KERN_INFO "ISA support compiled in.\n"); #endif /* Warning : BlackMagicAhead(tm). Please wave your favorite dead chicken over the busses */ /* First say hello to the EIU */ outl(0x0000FFFF, EIU_PREMPT_REG); outl(1, EIU_QUIET_REG); outl(0x40f3c07F, EIU_MODE_REG); /* Now be nice to the EISA chipset */ outb(1, EISA_EXT_NMI_RESET_CTRL); udelay(50); /* Wait long enough for the dust to settle */ outb(0, EISA_EXT_NMI_RESET_CTRL); outb(0, EISA_DMA2_WRITE_SINGLE); init_i8259_irqs(); if (request_irq(SGI_EISA_IRQ, ip22_eisa_intr, 0, "EISA", NULL)) pr_err("Failed to request irq %d (EISA)\n", SGI_EISA_IRQ); EISA_bus = 1; return 0; }
linux-master
arch/mips/sgi-ip22/ip22-eisa.c
// SPDX-License-Identifier: GPL-2.0 /* * ip28-berr.c: Bus error handling. * * Copyright (C) 2002, 2003 Ladislav Michl ([email protected]) * Copyright (C) 2005 Peter Fuerst ([email protected]) - IP28 */ #include <linux/init.h> #include <linux/kernel.h> #include <linux/mm.h> #include <linux/sched.h> #include <linux/sched/debug.h> #include <linux/sched/signal.h> #include <linux/seq_file.h> #include <asm/addrspace.h> #include <asm/traps.h> #include <asm/branch.h> #include <asm/irq_regs.h> #include <asm/sgi/mc.h> #include <asm/sgi/hpc3.h> #include <asm/sgi/ioc.h> #include <asm/sgi/ip22.h> #include <asm/r4kcache.h> #include <linux/uaccess.h> #include <asm/bootinfo.h> static unsigned int count_be_is_fixup; static unsigned int count_be_handler; static unsigned int count_be_interrupt; static int debug_be_interrupt; static unsigned int cpu_err_stat; /* Status reg for CPU */ static unsigned int gio_err_stat; /* Status reg for GIO */ static unsigned int cpu_err_addr; /* Error address reg for CPU */ static unsigned int gio_err_addr; /* Error address reg for GIO */ static unsigned int extio_stat; static unsigned int hpc3_berr_stat; /* Bus error interrupt status */ struct hpc3_stat { unsigned long addr; unsigned int ctrl; unsigned int cbp; unsigned int ndptr; }; static struct { struct hpc3_stat pbdma[8]; struct hpc3_stat scsi[2]; struct hpc3_stat ethrx, ethtx; } hpc3; static struct { unsigned long err_addr; struct { u32 lo; u32 hi; } tags[1][2], tagd[4][2], tagi[4][2]; /* Way 0/1 */ } cache_tags; static inline void save_cache_tags(unsigned busaddr) { unsigned long addr = CAC_BASE | busaddr; int i; cache_tags.err_addr = addr; /* * Starting with a bus-address, save secondary cache (indexed by * PA[23..18:7..6]) tags first. */ addr &= ~1L; #define tag cache_tags.tags[0] cache_op(Index_Load_Tag_S, addr); tag[0].lo = read_c0_taglo(); /* PA[35:18], VA[13:12] */ tag[0].hi = read_c0_taghi(); /* PA[39:36] */ cache_op(Index_Load_Tag_S, addr | 1L); tag[1].lo = read_c0_taglo(); /* PA[35:18], VA[13:12] */ tag[1].hi = read_c0_taghi(); /* PA[39:36] */ #undef tag /* * Save all primary data cache (indexed by VA[13:5]) tags which * might fit to this bus-address, knowing that VA[11:0] == PA[11:0]. * Saving all tags and evaluating them later is easier and safer * than relying on VA[13:12] from the secondary cache tags to pick * matching primary tags here already. */ addr &= (0xffL << 56) | ((1 << 12) - 1); #define tag cache_tags.tagd[i] for (i = 0; i < 4; ++i, addr += (1 << 12)) { cache_op(Index_Load_Tag_D, addr); tag[0].lo = read_c0_taglo(); /* PA[35:12] */ tag[0].hi = read_c0_taghi(); /* PA[39:36] */ cache_op(Index_Load_Tag_D, addr | 1L); tag[1].lo = read_c0_taglo(); /* PA[35:12] */ tag[1].hi = read_c0_taghi(); /* PA[39:36] */ } #undef tag /* * Save primary instruction cache (indexed by VA[13:6]) tags * the same way. */ addr &= (0xffL << 56) | ((1 << 12) - 1); #define tag cache_tags.tagi[i] for (i = 0; i < 4; ++i, addr += (1 << 12)) { cache_op(Index_Load_Tag_I, addr); tag[0].lo = read_c0_taglo(); /* PA[35:12] */ tag[0].hi = read_c0_taghi(); /* PA[39:36] */ cache_op(Index_Load_Tag_I, addr | 1L); tag[1].lo = read_c0_taglo(); /* PA[35:12] */ tag[1].hi = read_c0_taghi(); /* PA[39:36] */ } #undef tag } #define GIO_ERRMASK 0xff00 #define CPU_ERRMASK 0x3f00 static void save_and_clear_buserr(void) { int i; /* save status registers */ cpu_err_addr = sgimc->cerr; cpu_err_stat = sgimc->cstat; gio_err_addr = sgimc->gerr; gio_err_stat = sgimc->gstat; extio_stat = sgioc->extio; hpc3_berr_stat = hpc3c0->bestat; hpc3.scsi[0].addr = (unsigned long)&hpc3c0->scsi_chan0; hpc3.scsi[0].ctrl = hpc3c0->scsi_chan0.ctrl; /* HPC3_SCTRL_ACTIVE ? */ hpc3.scsi[0].cbp = hpc3c0->scsi_chan0.cbptr; hpc3.scsi[0].ndptr = hpc3c0->scsi_chan0.ndptr; hpc3.scsi[1].addr = (unsigned long)&hpc3c0->scsi_chan1; hpc3.scsi[1].ctrl = hpc3c0->scsi_chan1.ctrl; /* HPC3_SCTRL_ACTIVE ? */ hpc3.scsi[1].cbp = hpc3c0->scsi_chan1.cbptr; hpc3.scsi[1].ndptr = hpc3c0->scsi_chan1.ndptr; hpc3.ethrx.addr = (unsigned long)&hpc3c0->ethregs.rx_cbptr; hpc3.ethrx.ctrl = hpc3c0->ethregs.rx_ctrl; /* HPC3_ERXCTRL_ACTIVE ? */ hpc3.ethrx.cbp = hpc3c0->ethregs.rx_cbptr; hpc3.ethrx.ndptr = hpc3c0->ethregs.rx_ndptr; hpc3.ethtx.addr = (unsigned long)&hpc3c0->ethregs.tx_cbptr; hpc3.ethtx.ctrl = hpc3c0->ethregs.tx_ctrl; /* HPC3_ETXCTRL_ACTIVE ? */ hpc3.ethtx.cbp = hpc3c0->ethregs.tx_cbptr; hpc3.ethtx.ndptr = hpc3c0->ethregs.tx_ndptr; for (i = 0; i < 8; ++i) { /* HPC3_PDMACTRL_ISACT ? */ hpc3.pbdma[i].addr = (unsigned long)&hpc3c0->pbdma[i]; hpc3.pbdma[i].ctrl = hpc3c0->pbdma[i].pbdma_ctrl; hpc3.pbdma[i].cbp = hpc3c0->pbdma[i].pbdma_bptr; hpc3.pbdma[i].ndptr = hpc3c0->pbdma[i].pbdma_dptr; } i = 0; if (gio_err_stat & CPU_ERRMASK) i = gio_err_addr; if (cpu_err_stat & CPU_ERRMASK) i = cpu_err_addr; save_cache_tags(i); sgimc->cstat = sgimc->gstat = 0; } static void print_cache_tags(void) { u32 scb, scw; int i; printk(KERN_ERR "Cache tags @ %08x:\n", (unsigned)cache_tags.err_addr); /* PA[31:12] shifted to PTag0 (PA[35:12]) format */ scw = (cache_tags.err_addr >> 4) & 0x0fffff00; scb = cache_tags.err_addr & ((1 << 12) - 1) & ~((1 << 5) - 1); for (i = 0; i < 4; ++i) { /* for each possible VA[13:12] value */ if ((cache_tags.tagd[i][0].lo & 0x0fffff00) != scw && (cache_tags.tagd[i][1].lo & 0x0fffff00) != scw) continue; printk(KERN_ERR "D: 0: %08x %08x, 1: %08x %08x (VA[13:5] %04x)\n", cache_tags.tagd[i][0].hi, cache_tags.tagd[i][0].lo, cache_tags.tagd[i][1].hi, cache_tags.tagd[i][1].lo, scb | (1 << 12)*i); } scb = cache_tags.err_addr & ((1 << 12) - 1) & ~((1 << 6) - 1); for (i = 0; i < 4; ++i) { /* for each possible VA[13:12] value */ if ((cache_tags.tagi[i][0].lo & 0x0fffff00) != scw && (cache_tags.tagi[i][1].lo & 0x0fffff00) != scw) continue; printk(KERN_ERR "I: 0: %08x %08x, 1: %08x %08x (VA[13:6] %04x)\n", cache_tags.tagi[i][0].hi, cache_tags.tagi[i][0].lo, cache_tags.tagi[i][1].hi, cache_tags.tagi[i][1].lo, scb | (1 << 12)*i); } i = read_c0_config(); scb = i & (1 << 13) ? 7:6; /* scblksize = 2^[7..6] */ scw = ((i >> 16) & 7) + 19 - 1; /* scwaysize = 2^[24..19] / 2 */ i = ((1 << scw) - 1) & ~((1 << scb) - 1); printk(KERN_ERR "S: 0: %08x %08x, 1: %08x %08x (PA[%u:%u] %05x)\n", cache_tags.tags[0][0].hi, cache_tags.tags[0][0].lo, cache_tags.tags[0][1].hi, cache_tags.tags[0][1].lo, scw-1, scb, i & (unsigned)cache_tags.err_addr); } static inline const char *cause_excode_text(int cause) { static const char *txt[32] = { "Interrupt", "TLB modification", "TLB (load or instruction fetch)", "TLB (store)", "Address error (load or instruction fetch)", "Address error (store)", "Bus error (instruction fetch)", "Bus error (data: load or store)", "Syscall", "Breakpoint", "Reserved instruction", "Coprocessor unusable", "Arithmetic Overflow", "Trap", "14", "Floating-Point", "16", "17", "18", "19", "20", "21", "22", "Watch Hi/Lo", "24", "25", "26", "27", "28", "29", "30", "31", }; return txt[(cause & 0x7c) >> 2]; } static void print_buserr(const struct pt_regs *regs) { const int field = 2 * sizeof(unsigned long); int error = 0; if (extio_stat & EXTIO_MC_BUSERR) { printk(KERN_ERR "MC Bus Error\n"); error |= 1; } if (extio_stat & EXTIO_HPC3_BUSERR) { printk(KERN_ERR "HPC3 Bus Error 0x%x:<id=0x%x,%s,lane=0x%x>\n", hpc3_berr_stat, (hpc3_berr_stat & HPC3_BESTAT_PIDMASK) >> HPC3_BESTAT_PIDSHIFT, (hpc3_berr_stat & HPC3_BESTAT_CTYPE) ? "PIO" : "DMA", hpc3_berr_stat & HPC3_BESTAT_BLMASK); error |= 2; } if (extio_stat & EXTIO_EISA_BUSERR) { printk(KERN_ERR "EISA Bus Error\n"); error |= 4; } if (cpu_err_stat & CPU_ERRMASK) { printk(KERN_ERR "CPU error 0x%x<%s%s%s%s%s%s> @ 0x%08x\n", cpu_err_stat, cpu_err_stat & SGIMC_CSTAT_RD ? "RD " : "", cpu_err_stat & SGIMC_CSTAT_PAR ? "PAR " : "", cpu_err_stat & SGIMC_CSTAT_ADDR ? "ADDR " : "", cpu_err_stat & SGIMC_CSTAT_SYSAD_PAR ? "SYSAD " : "", cpu_err_stat & SGIMC_CSTAT_SYSCMD_PAR ? "SYSCMD " : "", cpu_err_stat & SGIMC_CSTAT_BAD_DATA ? "BAD_DATA " : "", cpu_err_addr); error |= 8; } if (gio_err_stat & GIO_ERRMASK) { printk(KERN_ERR "GIO error 0x%x:<%s%s%s%s%s%s%s%s> @ 0x%08x\n", gio_err_stat, gio_err_stat & SGIMC_GSTAT_RD ? "RD " : "", gio_err_stat & SGIMC_GSTAT_WR ? "WR " : "", gio_err_stat & SGIMC_GSTAT_TIME ? "TIME " : "", gio_err_stat & SGIMC_GSTAT_PROM ? "PROM " : "", gio_err_stat & SGIMC_GSTAT_ADDR ? "ADDR " : "", gio_err_stat & SGIMC_GSTAT_BC ? "BC " : "", gio_err_stat & SGIMC_GSTAT_PIO_RD ? "PIO_RD " : "", gio_err_stat & SGIMC_GSTAT_PIO_WR ? "PIO_WR " : "", gio_err_addr); error |= 16; } if (!error) printk(KERN_ERR "MC: Hmm, didn't find any error condition.\n"); else { printk(KERN_ERR "CP0: config %08x, " "MC: cpuctrl0/1: %08x/%05x, giopar: %04x\n" "MC: cpu/gio_memacc: %08x/%05x, memcfg0/1: %08x/%08x\n", read_c0_config(), sgimc->cpuctrl0, sgimc->cpuctrl0, sgimc->giopar, sgimc->cmacc, sgimc->gmacc, sgimc->mconfig0, sgimc->mconfig1); print_cache_tags(); } printk(KERN_ALERT "%s, epc == %0*lx, ra == %0*lx\n", cause_excode_text(regs->cp0_cause), field, regs->cp0_epc, field, regs->regs[31]); } static int check_microtlb(u32 hi, u32 lo, unsigned long vaddr) { /* This is likely rather similar to correct code ;-) */ vaddr &= 0x7fffffff; /* Doc. states that top bit is ignored */ /* If tlb-entry is valid and VPN-high (bits [30:21] ?) matches... */ if ((lo & 2) && (vaddr >> 21) == ((hi<<1) >> 22)) { u32 ctl = sgimc->dma_ctrl; if (ctl & 1) { unsigned int pgsz = (ctl & 2) ? 14:12; /* 16k:4k */ /* PTEIndex is VPN-low (bits [22:14]/[20:12] ?) */ unsigned long pte = (lo >> 6) << 12; /* PTEBase */ pte += 8*((vaddr >> pgsz) & 0x1ff); if (page_is_ram(PFN_DOWN(pte))) { /* * Note: Since DMA hardware does look up * translation on its own, this PTE *must* * match the TLB/EntryLo-register format ! */ unsigned long a = *(unsigned long *) PHYS_TO_XKSEG_UNCACHED(pte); a = (a & 0x3f) << 6; /* PFN */ a += vaddr & ((1 << pgsz) - 1); return cpu_err_addr == a; } } } return 0; } static int check_vdma_memaddr(void) { if (cpu_err_stat & CPU_ERRMASK) { u32 a = sgimc->maddronly; if (!(sgimc->dma_ctrl & 0x100)) /* Xlate-bit clear ? */ return cpu_err_addr == a; if (check_microtlb(sgimc->dtlb_hi0, sgimc->dtlb_lo0, a) || check_microtlb(sgimc->dtlb_hi1, sgimc->dtlb_lo1, a) || check_microtlb(sgimc->dtlb_hi2, sgimc->dtlb_lo2, a) || check_microtlb(sgimc->dtlb_hi3, sgimc->dtlb_lo3, a)) return 1; } return 0; } static int check_vdma_gioaddr(void) { if (gio_err_stat & GIO_ERRMASK) { u32 a = sgimc->gio_dma_trans; a = (sgimc->gmaddronly & ~a) | (sgimc->gio_dma_sbits & a); return gio_err_addr == a; } return 0; } /* * MC sends an interrupt whenever bus or parity errors occur. In addition, * if the error happened during a CPU read, it also asserts the bus error * pin on the R4K. Code in bus error handler save the MC bus error registers * and then clear the interrupt when this happens. */ static int ip28_be_interrupt(const struct pt_regs *regs) { int i; save_and_clear_buserr(); /* * Try to find out, whether we got here by a mispredicted speculative * load/store operation. If so, it's not fatal, we can go on. */ /* Any cause other than "Interrupt" (ExcCode 0) is fatal. */ if (regs->cp0_cause & CAUSEF_EXCCODE) goto mips_be_fatal; /* Any cause other than "Bus error interrupt" (IP6) is weird. */ if ((regs->cp0_cause & CAUSEF_IP6) != CAUSEF_IP6) goto mips_be_fatal; if (extio_stat & (EXTIO_HPC3_BUSERR | EXTIO_EISA_BUSERR)) goto mips_be_fatal; /* Any state other than "Memory bus error" is fatal. */ if (cpu_err_stat & CPU_ERRMASK & ~SGIMC_CSTAT_ADDR) goto mips_be_fatal; /* GIO errors other than timeouts are fatal */ if (gio_err_stat & GIO_ERRMASK & ~SGIMC_GSTAT_TIME) goto mips_be_fatal; /* * Now we have an asynchronous bus error, speculatively or DMA caused. * Need to search all DMA descriptors for the error address. */ for (i = 0; i < sizeof(hpc3)/sizeof(struct hpc3_stat); ++i) { struct hpc3_stat *hp = (struct hpc3_stat *)&hpc3 + i; if ((cpu_err_stat & CPU_ERRMASK) && (cpu_err_addr == hp->ndptr || cpu_err_addr == hp->cbp)) break; if ((gio_err_stat & GIO_ERRMASK) && (gio_err_addr == hp->ndptr || gio_err_addr == hp->cbp)) break; } if (i < sizeof(hpc3)/sizeof(struct hpc3_stat)) { struct hpc3_stat *hp = (struct hpc3_stat *)&hpc3 + i; printk(KERN_ERR "at DMA addresses: HPC3 @ %08lx:" " ctl %08x, ndp %08x, cbp %08x\n", CPHYSADDR(hp->addr), hp->ctrl, hp->ndptr, hp->cbp); goto mips_be_fatal; } /* Check MC's virtual DMA stuff. */ if (check_vdma_memaddr()) { printk(KERN_ERR "at GIO DMA: mem address 0x%08x.\n", sgimc->maddronly); goto mips_be_fatal; } if (check_vdma_gioaddr()) { printk(KERN_ERR "at GIO DMA: gio address 0x%08x.\n", sgimc->gmaddronly); goto mips_be_fatal; } /* A speculative bus error... */ if (debug_be_interrupt) { print_buserr(regs); printk(KERN_ERR "discarded!\n"); } return MIPS_BE_DISCARD; mips_be_fatal: print_buserr(regs); return MIPS_BE_FATAL; } void ip22_be_interrupt(int irq) { struct pt_regs *regs = get_irq_regs(); count_be_interrupt++; if (ip28_be_interrupt(regs) != MIPS_BE_DISCARD) { /* Assume it would be too dangerous to continue ... */ die_if_kernel("Oops", regs); force_sig(SIGBUS); } else if (debug_be_interrupt) show_regs(regs); } static int ip28_be_handler(struct pt_regs *regs, int is_fixup) { /* * We arrive here only in the unusual case of do_be() invocation, * i.e. by a bus error exception without a bus error interrupt. */ if (is_fixup) { count_be_is_fixup++; save_and_clear_buserr(); return MIPS_BE_FIXUP; } count_be_handler++; return ip28_be_interrupt(regs); } void __init ip22_be_init(void) { mips_set_be_handler(ip28_be_handler); } int ip28_show_be_info(struct seq_file *m) { seq_printf(m, "IP28 be fixups\t\t: %u\n", count_be_is_fixup); seq_printf(m, "IP28 be interrupts\t: %u\n", count_be_interrupt); seq_printf(m, "IP28 be handler\t\t: %u\n", count_be_handler); return 0; } static int __init debug_be_setup(char *str) { debug_be_interrupt++; return 1; } __setup("ip28_debug_be", debug_be_setup);
linux-master
arch/mips/sgi-ip22/ip28-berr.c
// SPDX-License-Identifier: GPL-2.0 /* * ip22-setup.c: SGI specific setup, including init of the feature struct. * * Copyright (C) 1996 David S. Miller ([email protected]) * Copyright (C) 1997, 1998 Ralf Baechle ([email protected]) */ #include <linux/init.h> #include <linux/kernel.h> #include <linux/kdev_t.h> #include <linux/types.h> #include <linux/console.h> #include <linux/sched.h> #include <linux/tty.h> #include <asm/addrspace.h> #include <asm/bcache.h> #include <asm/bootinfo.h> #include <asm/irq.h> #include <asm/reboot.h> #include <asm/time.h> #include <asm/io.h> #include <asm/traps.h> #include <asm/sgialib.h> #include <asm/sgi/mc.h> #include <asm/sgi/hpc3.h> #include <asm/sgi/ip22.h> extern void ip22_be_init(void) __init; void __init plat_mem_setup(void) { char *ctype; char *cserial; board_be_init = ip22_be_init; /* Init the INDY HPC I/O controller. Need to call this before * fucking with the memory controller because it needs to know the * boardID and whether this is a Guiness or a FullHouse machine. */ sgihpc_init(); /* Init INDY memory controller. */ sgimc_init(); #ifdef CONFIG_BOARD_SCACHE /* Now enable boardcaches, if any. */ indy_sc_init(); #endif /* Set EISA IO port base for Indigo2 * ioremap cannot fail */ set_io_port_base((unsigned long)ioremap(0x00080000, 0x1fffffff - 0x00080000)); /* ARCS console environment variable is set to "g?" for * graphics console, it is set to "d" for the first serial * line and "d2" for the second serial line. * * Need to check if the case is 'g' but no keyboard: * (ConsoleIn/Out = serial) */ ctype = ArcGetEnvironmentVariable("console"); cserial = ArcGetEnvironmentVariable("ConsoleOut"); if ((ctype && *ctype == 'd') || (cserial && *cserial == 's')) { static char options[8] __initdata; char *baud = ArcGetEnvironmentVariable("dbaud"); if (baud) strcpy(options, baud); add_preferred_console("ttyS", *(ctype + 1) == '2' ? 1 : 0, baud ? options : NULL); } else if (!ctype || *ctype != 'g') { /* Use ARC if we don't want serial ('d') or graphics ('g'). */ prom_flags |= PROM_FLAG_USE_AS_CONSOLE; add_preferred_console("arc", 0, NULL); } }
linux-master
arch/mips/sgi-ip22/ip22-setup.c
// SPDX-License-Identifier: GPL-2.0 /* * ip22-nvram.c: NVRAM and serial EEPROM handling. * * Copyright (C) 2003 Ladislav Michl ([email protected]) */ #include <linux/export.h> #include <asm/sgi/hpc3.h> #include <asm/sgi/ip22.h> /* Control opcode for serial eeprom */ #define EEPROM_READ 0xc000 /* serial memory read */ #define EEPROM_WEN 0x9800 /* write enable before prog modes */ #define EEPROM_WRITE 0xa000 /* serial memory write */ #define EEPROM_WRALL 0x8800 /* write all registers */ #define EEPROM_WDS 0x8000 /* disable all programming */ #define EEPROM_PRREAD 0xc000 /* read protect register */ #define EEPROM_PREN 0x9800 /* enable protect register mode */ #define EEPROM_PRCLEAR 0xffff /* clear protect register */ #define EEPROM_PRWRITE 0xa000 /* write protect register */ #define EEPROM_PRDS 0x8000 /* disable protect register, forever */ #define EEPROM_EPROT 0x01 /* Protect register enable */ #define EEPROM_CSEL 0x02 /* Chip select */ #define EEPROM_ECLK 0x04 /* EEPROM clock */ #define EEPROM_DATO 0x08 /* Data out */ #define EEPROM_DATI 0x10 /* Data in */ /* We need to use these functions early... */ #define delay() ({ \ int x; \ for (x=0; x<100000; x++) __asm__ __volatile__(""); }) #define eeprom_cs_on(ptr) ({ \ __raw_writel(__raw_readl(ptr) & ~EEPROM_DATO, ptr); \ __raw_writel(__raw_readl(ptr) & ~EEPROM_ECLK, ptr); \ __raw_writel(__raw_readl(ptr) & ~EEPROM_EPROT, ptr); \ delay(); \ __raw_writel(__raw_readl(ptr) | EEPROM_CSEL, ptr); \ __raw_writel(__raw_readl(ptr) | EEPROM_ECLK, ptr); }) #define eeprom_cs_off(ptr) ({ \ __raw_writel(__raw_readl(ptr) & ~EEPROM_ECLK, ptr); \ __raw_writel(__raw_readl(ptr) & ~EEPROM_CSEL, ptr); \ __raw_writel(__raw_readl(ptr) | EEPROM_EPROT, ptr); \ __raw_writel(__raw_readl(ptr) | EEPROM_ECLK, ptr); }) #define BITS_IN_COMMAND 11 /* * clock in the nvram command and the register number. For the * national semiconductor nv ram chip the op code is 3 bits and * the address is 6/8 bits. */ static inline void eeprom_cmd(unsigned int *ctrl, unsigned cmd, unsigned reg) { unsigned short ser_cmd; int i; ser_cmd = cmd | (reg << (16 - BITS_IN_COMMAND)); for (i = 0; i < BITS_IN_COMMAND; i++) { if (ser_cmd & (1<<15)) /* if high order bit set */ __raw_writel(__raw_readl(ctrl) | EEPROM_DATO, ctrl); else __raw_writel(__raw_readl(ctrl) & ~EEPROM_DATO, ctrl); __raw_writel(__raw_readl(ctrl) & ~EEPROM_ECLK, ctrl); delay(); __raw_writel(__raw_readl(ctrl) | EEPROM_ECLK, ctrl); delay(); ser_cmd <<= 1; } /* see data sheet timing diagram */ __raw_writel(__raw_readl(ctrl) & ~EEPROM_DATO, ctrl); } unsigned short ip22_eeprom_read(unsigned int *ctrl, int reg) { unsigned short res = 0; int i; __raw_writel(__raw_readl(ctrl) & ~EEPROM_EPROT, ctrl); eeprom_cs_on(ctrl); eeprom_cmd(ctrl, EEPROM_READ, reg); /* clock the data ouf of serial mem */ for (i = 0; i < 16; i++) { __raw_writel(__raw_readl(ctrl) & ~EEPROM_ECLK, ctrl); delay(); __raw_writel(__raw_readl(ctrl) | EEPROM_ECLK, ctrl); delay(); res <<= 1; if (__raw_readl(ctrl) & EEPROM_DATI) res |= 1; } eeprom_cs_off(ctrl); return res; } EXPORT_SYMBOL(ip22_eeprom_read); /* * Read specified register from main NVRAM */ unsigned short ip22_nvram_read(int reg) { if (ip22_is_fullhouse()) /* IP22 (Indigo2 aka FullHouse) stores env variables into * 93CS56 Microwire Bus EEPROM 2048 Bit (128x16) */ return ip22_eeprom_read(&hpc3c0->eeprom, reg); else { unsigned short tmp; /* IP24 (Indy aka Guiness) uses DS1386 8K version */ reg <<= 1; tmp = hpc3c0->bbram[reg++] & 0xff; return (tmp << 8) | (hpc3c0->bbram[reg] & 0xff); } } EXPORT_SYMBOL(ip22_nvram_read);
linux-master
arch/mips/sgi-ip22/ip22-nvram.c
// SPDX-License-Identifier: GPL-2.0 /* * ip22-berr.c: Bus error handling. * * Copyright (C) 2002, 2003 Ladislav Michl ([email protected]) */ #include <linux/init.h> #include <linux/kernel.h> #include <linux/sched/signal.h> #include <asm/addrspace.h> #include <asm/traps.h> #include <asm/branch.h> #include <asm/irq_regs.h> #include <asm/sgi/mc.h> #include <asm/sgi/hpc3.h> #include <asm/sgi/ioc.h> #include <asm/sgi/ip22.h> static unsigned int cpu_err_stat; /* Status reg for CPU */ static unsigned int gio_err_stat; /* Status reg for GIO */ static unsigned int cpu_err_addr; /* Error address reg for CPU */ static unsigned int gio_err_addr; /* Error address reg for GIO */ static unsigned int extio_stat; static unsigned int hpc3_berr_stat; /* Bus error interrupt status */ static void save_and_clear_buserr(void) { /* save status registers */ cpu_err_addr = sgimc->cerr; cpu_err_stat = sgimc->cstat; gio_err_addr = sgimc->gerr; gio_err_stat = sgimc->gstat; extio_stat = ip22_is_fullhouse() ? sgioc->extio : (sgint->errstat << 4); hpc3_berr_stat = hpc3c0->bestat; sgimc->cstat = sgimc->gstat = 0; } #define GIO_ERRMASK 0xff00 #define CPU_ERRMASK 0x3f00 static void print_buserr(void) { if (extio_stat & EXTIO_MC_BUSERR) printk(KERN_ERR "MC Bus Error\n"); if (extio_stat & EXTIO_HPC3_BUSERR) printk(KERN_ERR "HPC3 Bus Error 0x%x:<id=0x%x,%s,lane=0x%x>\n", hpc3_berr_stat, (hpc3_berr_stat & HPC3_BESTAT_PIDMASK) >> HPC3_BESTAT_PIDSHIFT, (hpc3_berr_stat & HPC3_BESTAT_CTYPE) ? "PIO" : "DMA", hpc3_berr_stat & HPC3_BESTAT_BLMASK); if (extio_stat & EXTIO_EISA_BUSERR) printk(KERN_ERR "EISA Bus Error\n"); if (cpu_err_stat & CPU_ERRMASK) printk(KERN_ERR "CPU error 0x%x<%s%s%s%s%s%s> @ 0x%08x\n", cpu_err_stat, cpu_err_stat & SGIMC_CSTAT_RD ? "RD " : "", cpu_err_stat & SGIMC_CSTAT_PAR ? "PAR " : "", cpu_err_stat & SGIMC_CSTAT_ADDR ? "ADDR " : "", cpu_err_stat & SGIMC_CSTAT_SYSAD_PAR ? "SYSAD " : "", cpu_err_stat & SGIMC_CSTAT_SYSCMD_PAR ? "SYSCMD " : "", cpu_err_stat & SGIMC_CSTAT_BAD_DATA ? "BAD_DATA " : "", cpu_err_addr); if (gio_err_stat & GIO_ERRMASK) printk(KERN_ERR "GIO error 0x%x:<%s%s%s%s%s%s%s%s> @ 0x%08x\n", gio_err_stat, gio_err_stat & SGIMC_GSTAT_RD ? "RD " : "", gio_err_stat & SGIMC_GSTAT_WR ? "WR " : "", gio_err_stat & SGIMC_GSTAT_TIME ? "TIME " : "", gio_err_stat & SGIMC_GSTAT_PROM ? "PROM " : "", gio_err_stat & SGIMC_GSTAT_ADDR ? "ADDR " : "", gio_err_stat & SGIMC_GSTAT_BC ? "BC " : "", gio_err_stat & SGIMC_GSTAT_PIO_RD ? "PIO_RD " : "", gio_err_stat & SGIMC_GSTAT_PIO_WR ? "PIO_WR " : "", gio_err_addr); } /* * MC sends an interrupt whenever bus or parity errors occur. In addition, * if the error happened during a CPU read, it also asserts the bus error * pin on the R4K. Code in bus error handler save the MC bus error registers * and then clear the interrupt when this happens. */ void ip22_be_interrupt(int irq) { const int field = 2 * sizeof(unsigned long); struct pt_regs *regs = get_irq_regs(); save_and_clear_buserr(); print_buserr(); printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n", (regs->cp0_cause & 4) ? "Data" : "Instruction", field, regs->cp0_epc, field, regs->regs[31]); /* Assume it would be too dangerous to continue ... */ die_if_kernel("Oops", regs); force_sig(SIGBUS); } static int ip22_be_handler(struct pt_regs *regs, int is_fixup) { save_and_clear_buserr(); if (is_fixup) return MIPS_BE_FIXUP; print_buserr(); return MIPS_BE_FATAL; } void __init ip22_be_init(void) { mips_set_be_handler(ip22_be_handler); }
linux-master
arch/mips/sgi-ip22/ip22-berr.c
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Time operations for IP22 machines. Original code may come from * Ralf Baechle or David S. Miller (sorry guys, i'm really not sure) * * Copyright (C) 2001 by Ladislav Michl * Copyright (C) 2003, 06 Ralf Baechle ([email protected]) */ #include <linux/bcd.h> #include <linux/i8253.h> #include <linux/init.h> #include <linux/irq.h> #include <linux/kernel.h> #include <linux/interrupt.h> #include <linux/kernel_stat.h> #include <linux/time.h> #include <linux/ftrace.h> #include <asm/cpu.h> #include <asm/mipsregs.h> #include <asm/io.h> #include <asm/irq.h> #include <asm/time.h> #include <asm/sgialib.h> #include <asm/sgi/ioc.h> #include <asm/sgi/hpc3.h> #include <asm/sgi/ip22.h> static unsigned long dosample(void) { u32 ct0, ct1; u8 msb; /* Start the counter. */ sgint->tcword = (SGINT_TCWORD_CNT2 | SGINT_TCWORD_CALL | SGINT_TCWORD_MRGEN); sgint->tcnt2 = SGINT_TCSAMP_COUNTER & 0xff; sgint->tcnt2 = SGINT_TCSAMP_COUNTER >> 8; /* Get initial counter invariant */ ct0 = read_c0_count(); /* Latch and spin until top byte of counter2 is zero */ do { writeb(SGINT_TCWORD_CNT2 | SGINT_TCWORD_CLAT, &sgint->tcword); (void) readb(&sgint->tcnt2); msb = readb(&sgint->tcnt2); ct1 = read_c0_count(); } while (msb); /* Stop the counter. */ writeb(SGINT_TCWORD_CNT2 | SGINT_TCWORD_CALL | SGINT_TCWORD_MSWST, &sgint->tcword); /* * Return the difference, this is how far the r4k counter increments * for every 1/HZ seconds. We round off the nearest 1 MHz of master * clock (= 1000000 / HZ / 2). */ return (ct1 - ct0) / (500000/HZ) * (500000/HZ); } /* * Here we need to calibrate the cycle counter to at least be close. */ __init void plat_time_init(void) { unsigned long r4k_ticks[3]; unsigned long r4k_tick; /* * Figure out the r4k offset, the algorithm is very simple and works in * _all_ cases as long as the 8254 counter register itself works ok (as * an interrupt driving timer it does not because of bug, this is why * we are using the onchip r4k counter/compare register to serve this * purpose, but for r4k_offset calculation it will work ok for us). * There are other very complicated ways of performing this calculation * but this one works just fine so I am not going to futz around. ;-) */ printk(KERN_INFO "Calibrating system timer... "); dosample(); /* Prime cache. */ dosample(); /* Prime cache. */ /* Zero is NOT an option. */ do { r4k_ticks[0] = dosample(); } while (!r4k_ticks[0]); do { r4k_ticks[1] = dosample(); } while (!r4k_ticks[1]); if (r4k_ticks[0] != r4k_ticks[1]) { printk("warning: timer counts differ, retrying... "); r4k_ticks[2] = dosample(); if (r4k_ticks[2] == r4k_ticks[0] || r4k_ticks[2] == r4k_ticks[1]) r4k_tick = r4k_ticks[2]; else { printk("disagreement, using average... "); r4k_tick = (r4k_ticks[0] + r4k_ticks[1] + r4k_ticks[2]) / 3; } } else r4k_tick = r4k_ticks[0]; printk("%d [%d.%04d MHz CPU]\n", (int) r4k_tick, (int) (r4k_tick / (500000 / HZ)), (int) (r4k_tick % (500000 / HZ))); mips_hpt_frequency = r4k_tick * HZ; if (ip22_is_fullhouse()) setup_pit_timer(); } /* Generic SGI handler for (spurious) 8254 interrupts */ void __irq_entry indy_8254timer_irq(void) { int irq = SGI_8254_0_IRQ; ULONG cnt; char c; irq_enter(); kstat_incr_irq_this_cpu(irq); printk(KERN_ALERT "Oops, got 8254 interrupt.\n"); ArcRead(0, &c, 1, &cnt); ArcEnterInteractiveMode(); irq_exit(); }
linux-master
arch/mips/sgi-ip22/ip22-time.c
// SPDX-License-Identifier: GPL-2.0-only #include <linux/export.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/slab.h> #include <asm/addrspace.h> #include <asm/paccess.h> #include <asm/gio_device.h> #include <asm/sgi/gio.h> #include <asm/sgi/hpc3.h> #include <asm/sgi/mc.h> #include <asm/sgi/ip22.h> static struct bus_type gio_bus_type; static struct { const char *name; __u8 id; } gio_name_table[] = { { .name = "SGI Impact", .id = 0x10 }, { .name = "Phobos G160", .id = 0x35 }, { .name = "Phobos G130", .id = 0x36 }, { .name = "Phobos G100", .id = 0x37 }, { .name = "Set Engineering GFE", .id = 0x38 }, /* fake IDs */ { .name = "SGI Newport", .id = 0x7e }, { .name = "SGI GR2/GR3", .id = 0x7f }, }; static void gio_bus_release(struct device *dev) { kfree(dev); } static struct device gio_bus = { .init_name = "gio", .release = &gio_bus_release, }; /** * gio_match_device - Tell if an of_device structure has a matching * gio_match structure * @ids: array of of device match structures to search in * @dev: the of device structure to match against * * Used by a driver to check whether an of_device present in the * system is in its list of supported devices. */ static const struct gio_device_id * gio_match_device(const struct gio_device_id *match, const struct gio_device *dev) { const struct gio_device_id *ids; for (ids = match; ids->id != 0xff; ids++) if (ids->id == dev->id.id) return ids; return NULL; } struct gio_device *gio_dev_get(struct gio_device *dev) { struct device *tmp; if (!dev) return NULL; tmp = get_device(&dev->dev); if (tmp) return to_gio_device(tmp); else return NULL; } EXPORT_SYMBOL_GPL(gio_dev_get); void gio_dev_put(struct gio_device *dev) { if (dev) put_device(&dev->dev); } EXPORT_SYMBOL_GPL(gio_dev_put); /** * gio_release_dev - free an gio device structure when all users of it are finished. * @dev: device that's been disconnected * * Will be called only by the device core when all users of this gio device are * done. */ void gio_release_dev(struct device *dev) { struct gio_device *giodev; giodev = to_gio_device(dev); kfree(giodev); } EXPORT_SYMBOL_GPL(gio_release_dev); int gio_device_register(struct gio_device *giodev) { giodev->dev.bus = &gio_bus_type; giodev->dev.parent = &gio_bus; return device_register(&giodev->dev); } EXPORT_SYMBOL_GPL(gio_device_register); void gio_device_unregister(struct gio_device *giodev) { device_unregister(&giodev->dev); } EXPORT_SYMBOL_GPL(gio_device_unregister); static int gio_bus_match(struct device *dev, struct device_driver *drv) { struct gio_device *gio_dev = to_gio_device(dev); struct gio_driver *gio_drv = to_gio_driver(drv); return gio_match_device(gio_drv->id_table, gio_dev) != NULL; } static int gio_device_probe(struct device *dev) { int error = -ENODEV; struct gio_driver *drv; struct gio_device *gio_dev; const struct gio_device_id *match; drv = to_gio_driver(dev->driver); gio_dev = to_gio_device(dev); if (!drv->probe) return error; gio_dev_get(gio_dev); match = gio_match_device(drv->id_table, gio_dev); if (match) error = drv->probe(gio_dev, match); if (error) gio_dev_put(gio_dev); return error; } static void gio_device_remove(struct device *dev) { struct gio_device *gio_dev = to_gio_device(dev); struct gio_driver *drv = to_gio_driver(dev->driver); if (drv->remove) drv->remove(gio_dev); } static void gio_device_shutdown(struct device *dev) { struct gio_device *gio_dev = to_gio_device(dev); struct gio_driver *drv = to_gio_driver(dev->driver); if (dev->driver && drv->shutdown) drv->shutdown(gio_dev); } static ssize_t modalias_show(struct device *dev, struct device_attribute *a, char *buf) { struct gio_device *gio_dev = to_gio_device(dev); int len = snprintf(buf, PAGE_SIZE, "gio:%x\n", gio_dev->id.id); return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; } static DEVICE_ATTR_RO(modalias); static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf) { struct gio_device *giodev; giodev = to_gio_device(dev); return sprintf(buf, "%s", giodev->name); } static DEVICE_ATTR_RO(name); static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf) { struct gio_device *giodev; giodev = to_gio_device(dev); return sprintf(buf, "%x", giodev->id.id); } static DEVICE_ATTR_RO(id); static struct attribute *gio_dev_attrs[] = { &dev_attr_modalias.attr, &dev_attr_name.attr, &dev_attr_id.attr, NULL, }; ATTRIBUTE_GROUPS(gio_dev); static int gio_device_uevent(const struct device *dev, struct kobj_uevent_env *env) { const struct gio_device *gio_dev = to_gio_device(dev); add_uevent_var(env, "MODALIAS=gio:%x", gio_dev->id.id); return 0; } int gio_register_driver(struct gio_driver *drv) { /* initialize common driver fields */ if (!drv->driver.name) drv->driver.name = drv->name; if (!drv->driver.owner) drv->driver.owner = drv->owner; drv->driver.bus = &gio_bus_type; /* register with core */ return driver_register(&drv->driver); } EXPORT_SYMBOL_GPL(gio_register_driver); void gio_unregister_driver(struct gio_driver *drv) { driver_unregister(&drv->driver); } EXPORT_SYMBOL_GPL(gio_unregister_driver); void gio_set_master(struct gio_device *dev) { u32 tmp = sgimc->giopar; switch (dev->slotno) { case 0: tmp |= SGIMC_GIOPAR_MASTERGFX; break; case 1: tmp |= SGIMC_GIOPAR_MASTEREXP0; break; case 2: tmp |= SGIMC_GIOPAR_MASTEREXP1; break; } sgimc->giopar = tmp; } EXPORT_SYMBOL_GPL(gio_set_master); void ip22_gio_set_64bit(int slotno) { u32 tmp = sgimc->giopar; switch (slotno) { case 0: tmp |= SGIMC_GIOPAR_GFX64; break; case 1: tmp |= SGIMC_GIOPAR_EXP064; break; case 2: tmp |= SGIMC_GIOPAR_EXP164; break; } sgimc->giopar = tmp; } static int ip22_gio_id(unsigned long addr, u32 *res) { u8 tmp8; u8 tmp16; u32 tmp32; u8 *ptr8; u16 *ptr16; u32 *ptr32; ptr32 = (void *)CKSEG1ADDR(addr); if (!get_dbe(tmp32, ptr32)) { /* * We got no DBE, but this doesn't mean anything. * If GIO is pipelined (which can't be disabled * for GFX slot) we don't get a DBE, but we see * the transfer size as data. So we do an 8bit * and a 16bit access and check whether the common * data matches */ ptr8 = (void *)CKSEG1ADDR(addr + 3); if (get_dbe(tmp8, ptr8)) { /* * 32bit access worked, but 8bit doesn't * so we don't see phantom reads on * a pipelined bus, but a real card which * doesn't support 8 bit reads */ *res = tmp32; return 1; } ptr16 = (void *)CKSEG1ADDR(addr + 2); get_dbe(tmp16, ptr16); if (tmp8 == (tmp16 & 0xff) && tmp8 == (tmp32 & 0xff) && tmp16 == (tmp32 & 0xffff)) { *res = tmp32; return 1; } } return 0; /* nothing here */ } #define HQ2_MYSTERY_OFFS 0x6A07C #define NEWPORT_USTATUS_OFFS 0xF133C static int ip22_is_gr2(unsigned long addr) { u32 tmp; u32 *ptr; /* HQ2 only allows 32bit accesses */ ptr = (void *)CKSEG1ADDR(addr + HQ2_MYSTERY_OFFS); if (!get_dbe(tmp, ptr)) { if (tmp == 0xdeadbeef) return 1; } return 0; } static void ip22_check_gio(int slotno, unsigned long addr, int irq) { const char *name = "Unknown"; struct gio_device *gio_dev; u32 tmp; __u8 id; int i; /* first look for GR2/GR3 by checking mystery register */ if (ip22_is_gr2(addr)) tmp = 0x7f; else { if (!ip22_gio_id(addr, &tmp)) { /* * no GIO signature at start address of slot * since Newport doesn't have one, we check if * user status register is readable */ if (ip22_gio_id(addr + NEWPORT_USTATUS_OFFS, &tmp)) tmp = 0x7e; else tmp = 0; } } if (tmp) { id = GIO_ID(tmp); if (tmp & GIO_32BIT_ID) { if (tmp & GIO_64BIT_IFACE) ip22_gio_set_64bit(slotno); } for (i = 0; i < ARRAY_SIZE(gio_name_table); i++) { if (id == gio_name_table[i].id) { name = gio_name_table[i].name; break; } } printk(KERN_INFO "GIO: slot %d : %s (id %x)\n", slotno, name, id); gio_dev = kzalloc(sizeof *gio_dev, GFP_KERNEL); if (!gio_dev) return; gio_dev->name = name; gio_dev->slotno = slotno; gio_dev->id.id = id; gio_dev->resource.start = addr; gio_dev->resource.end = addr + 0x3fffff; gio_dev->resource.flags = IORESOURCE_MEM; gio_dev->irq = irq; dev_set_name(&gio_dev->dev, "%d", slotno); gio_device_register(gio_dev); } else printk(KERN_INFO "GIO: slot %d : Empty\n", slotno); } static struct bus_type gio_bus_type = { .name = "gio", .dev_groups = gio_dev_groups, .match = gio_bus_match, .probe = gio_device_probe, .remove = gio_device_remove, .shutdown = gio_device_shutdown, .uevent = gio_device_uevent, }; static struct resource gio_bus_resource = { .start = GIO_SLOT_GFX_BASE, .end = GIO_SLOT_GFX_BASE + 0x9fffff, .name = "GIO Bus", .flags = IORESOURCE_MEM, }; int __init ip22_gio_init(void) { unsigned int pbdma __maybe_unused; int ret; ret = device_register(&gio_bus); if (ret) { put_device(&gio_bus); return ret; } ret = bus_register(&gio_bus_type); if (!ret) { request_resource(&iomem_resource, &gio_bus_resource); printk(KERN_INFO "GIO: Probing bus...\n"); if (ip22_is_fullhouse()) { /* Indigo2 */ ip22_check_gio(0, GIO_SLOT_GFX_BASE, SGI_GIO_1_IRQ); ip22_check_gio(1, GIO_SLOT_EXP0_BASE, SGI_GIO_1_IRQ); } else { /* Indy/Challenge S */ if (get_dbe(pbdma, (unsigned int *)&hpc3c1->pbdma[1])) ip22_check_gio(0, GIO_SLOT_GFX_BASE, SGI_GIO_0_IRQ); ip22_check_gio(1, GIO_SLOT_EXP0_BASE, SGI_GIOEXP0_IRQ); ip22_check_gio(2, GIO_SLOT_EXP1_BASE, SGI_GIOEXP1_IRQ); } } else device_unregister(&gio_bus); return ret; } subsys_initcall(ip22_gio_init);
linux-master
arch/mips/sgi-ip22/ip22-gio.c
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 1997, 1998, 2001, 03, 05, 06 by Ralf Baechle */ #include <linux/linkage.h> #include <linux/init.h> #include <linux/rtc/ds1286.h> #include <linux/interrupt.h> #include <linux/kernel.h> #include <linux/sched/signal.h> #include <linux/panic_notifier.h> #include <linux/pm.h> #include <linux/timer.h> #include <asm/io.h> #include <asm/irq.h> #include <asm/reboot.h> #include <asm/sgialib.h> #include <asm/sgi/ioc.h> #include <asm/sgi/hpc3.h> #include <asm/sgi/mc.h> #include <asm/sgi/ip22.h> /* * Just powerdown if init hasn't done after POWERDOWN_TIMEOUT seconds. * I'm not sure if this feature is a good idea, for now it's here just to * make the power button make behave just like under IRIX. */ #define POWERDOWN_TIMEOUT 120 /* * Blink frequency during reboot grace period and when panicked. */ #define POWERDOWN_FREQ (HZ / 4) #define PANIC_FREQ (HZ / 8) static struct timer_list power_timer, blink_timer, debounce_timer; static unsigned long blink_timer_timeout; #define MACHINE_PANICKED 1 #define MACHINE_SHUTTING_DOWN 2 static int machine_state; static void __noreturn sgi_machine_power_off(void) { unsigned int tmp; local_irq_disable(); /* Disable watchdog */ tmp = hpc3c0->rtcregs[RTC_CMD] & 0xff; hpc3c0->rtcregs[RTC_CMD] = tmp | RTC_WAM; hpc3c0->rtcregs[RTC_WSEC] = 0; hpc3c0->rtcregs[RTC_WHSEC] = 0; while (1) { sgioc->panel = ~SGIOC_PANEL_POWERON; /* Good bye cruel world ... */ /* If we're still running, we probably got sent an alarm interrupt. Read the flag to clear it. */ tmp = hpc3c0->rtcregs[RTC_HOURS_ALARM]; } } static void __noreturn sgi_machine_restart(char *command) { if (machine_state & MACHINE_SHUTTING_DOWN) sgi_machine_power_off(); sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT; while (1); } static void __noreturn sgi_machine_halt(void) { if (machine_state & MACHINE_SHUTTING_DOWN) sgi_machine_power_off(); ArcEnterInteractiveMode(); } static void power_timeout(struct timer_list *unused) { sgi_machine_power_off(); } static void blink_timeout(struct timer_list *unused) { /* XXX fix this for fullhouse */ sgi_ioc_reset ^= (SGIOC_RESET_LC0OFF|SGIOC_RESET_LC1OFF); sgioc->reset = sgi_ioc_reset; mod_timer(&blink_timer, jiffies + blink_timer_timeout); } static void debounce(struct timer_list *unused) { del_timer(&debounce_timer); if (sgint->istat1 & SGINT_ISTAT1_PWR) { /* Interrupt still being sent. */ debounce_timer.expires = jiffies + (HZ / 20); /* 0.05s */ add_timer(&debounce_timer); sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR | SGIOC_PANEL_VOLDNINTR | SGIOC_PANEL_VOLDNHOLD | SGIOC_PANEL_VOLUPINTR | SGIOC_PANEL_VOLUPHOLD; return; } if (machine_state & MACHINE_PANICKED) sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT; enable_irq(SGI_PANEL_IRQ); } static inline void power_button(void) { if (machine_state & MACHINE_PANICKED) return; if ((machine_state & MACHINE_SHUTTING_DOWN) || kill_cad_pid(SIGINT, 1)) { /* No init process or button pressed twice. */ sgi_machine_power_off(); } machine_state |= MACHINE_SHUTTING_DOWN; blink_timer_timeout = POWERDOWN_FREQ; blink_timeout(&blink_timer); timer_setup(&power_timer, power_timeout, 0); power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ; add_timer(&power_timer); } static irqreturn_t panel_int(int irq, void *dev_id) { unsigned int buttons; buttons = sgioc->panel; sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR; if (sgint->istat1 & SGINT_ISTAT1_PWR) { /* Wait until interrupt goes away */ disable_irq_nosync(SGI_PANEL_IRQ); timer_setup(&debounce_timer, debounce, 0); debounce_timer.expires = jiffies + 5; add_timer(&debounce_timer); } /* Power button was pressed * ioc.ps page 22: "The Panel Register is called Power Control by Full * House. Only lowest 2 bits are used. Guiness uses upper four bits * for volume control". This is not true, all bits are pulled high * on fullhouse */ if (!(buttons & SGIOC_PANEL_POWERINTR)) power_button(); return IRQ_HANDLED; } static int panic_event(struct notifier_block *this, unsigned long event, void *ptr) { if (machine_state & MACHINE_PANICKED) return NOTIFY_DONE; machine_state |= MACHINE_PANICKED; blink_timer_timeout = PANIC_FREQ; blink_timeout(&blink_timer); return NOTIFY_DONE; } static struct notifier_block panic_block = { .notifier_call = panic_event, }; static int __init reboot_setup(void) { int res; _machine_restart = sgi_machine_restart; _machine_halt = sgi_machine_halt; pm_power_off = sgi_machine_power_off; res = request_irq(SGI_PANEL_IRQ, panel_int, 0, "Front Panel", NULL); if (res) { printk(KERN_ERR "Allocation of front panel IRQ failed\n"); return res; } timer_setup(&blink_timer, blink_timeout, 0); atomic_notifier_chain_register(&panic_notifier_list, &panic_block); return 0; } subsys_initcall(reboot_setup);
linux-master
arch/mips/sgi-ip22/ip22-reset.c
// SPDX-License-Identifier: GPL-2.0 /* * ip22-int.c: Routines for generic manipulation of the INT[23] ASIC * found on INDY and Indigo2 workstations. * * Copyright (C) 1996 David S. Miller ([email protected]) * Copyright (C) 1997, 1998 Ralf Baechle ([email protected]) * Copyright (C) 1999 Andrew R. Baker ([email protected]) * - Indigo2 changes * - Interrupt handling fixes * Copyright (C) 2001, 2003 Ladislav Michl ([email protected]) */ #include <linux/types.h> #include <linux/init.h> #include <linux/kernel_stat.h> #include <linux/interrupt.h> #include <linux/ftrace.h> #include <asm/irq_cpu.h> #include <asm/sgi/hpc3.h> #include <asm/sgi/ip22.h> /* So far nothing hangs here */ #undef USE_LIO3_IRQ struct sgint_regs *sgint; static char lc0msk_to_irqnr[256]; static char lc1msk_to_irqnr[256]; static char lc2msk_to_irqnr[256]; static char lc3msk_to_irqnr[256]; extern int ip22_eisa_init(void); static void enable_local0_irq(struct irq_data *d) { /* don't allow mappable interrupt to be enabled from setup_irq, * we have our own way to do so */ if (d->irq != SGI_MAP_0_IRQ) sgint->imask0 |= (1 << (d->irq - SGINT_LOCAL0)); } static void disable_local0_irq(struct irq_data *d) { sgint->imask0 &= ~(1 << (d->irq - SGINT_LOCAL0)); } static struct irq_chip ip22_local0_irq_type = { .name = "IP22 local 0", .irq_mask = disable_local0_irq, .irq_unmask = enable_local0_irq, }; static void enable_local1_irq(struct irq_data *d) { /* don't allow mappable interrupt to be enabled from setup_irq, * we have our own way to do so */ if (d->irq != SGI_MAP_1_IRQ) sgint->imask1 |= (1 << (d->irq - SGINT_LOCAL1)); } static void disable_local1_irq(struct irq_data *d) { sgint->imask1 &= ~(1 << (d->irq - SGINT_LOCAL1)); } static struct irq_chip ip22_local1_irq_type = { .name = "IP22 local 1", .irq_mask = disable_local1_irq, .irq_unmask = enable_local1_irq, }; static void enable_local2_irq(struct irq_data *d) { sgint->imask0 |= (1 << (SGI_MAP_0_IRQ - SGINT_LOCAL0)); sgint->cmeimask0 |= (1 << (d->irq - SGINT_LOCAL2)); } static void disable_local2_irq(struct irq_data *d) { sgint->cmeimask0 &= ~(1 << (d->irq - SGINT_LOCAL2)); if (!sgint->cmeimask0) sgint->imask0 &= ~(1 << (SGI_MAP_0_IRQ - SGINT_LOCAL0)); } static struct irq_chip ip22_local2_irq_type = { .name = "IP22 local 2", .irq_mask = disable_local2_irq, .irq_unmask = enable_local2_irq, }; static void enable_local3_irq(struct irq_data *d) { sgint->imask1 |= (1 << (SGI_MAP_1_IRQ - SGINT_LOCAL1)); sgint->cmeimask1 |= (1 << (d->irq - SGINT_LOCAL3)); } static void disable_local3_irq(struct irq_data *d) { sgint->cmeimask1 &= ~(1 << (d->irq - SGINT_LOCAL3)); if (!sgint->cmeimask1) sgint->imask1 &= ~(1 << (SGI_MAP_1_IRQ - SGINT_LOCAL1)); } static struct irq_chip ip22_local3_irq_type = { .name = "IP22 local 3", .irq_mask = disable_local3_irq, .irq_unmask = enable_local3_irq, }; static void indy_local0_irqdispatch(void) { u8 mask = sgint->istat0 & sgint->imask0; u8 mask2; int irq; if (mask & SGINT_ISTAT0_LIO2) { mask2 = sgint->vmeistat & sgint->cmeimask0; irq = lc2msk_to_irqnr[mask2]; } else irq = lc0msk_to_irqnr[mask]; /* * workaround for INT2 bug; if irq == 0, INT2 has seen a fifo full * irq, but failed to latch it into status register */ if (irq) do_IRQ(irq); else do_IRQ(SGINT_LOCAL0 + 0); } static void indy_local1_irqdispatch(void) { u8 mask = sgint->istat1 & sgint->imask1; u8 mask2; int irq; if (mask & SGINT_ISTAT1_LIO3) { mask2 = sgint->vmeistat & sgint->cmeimask1; irq = lc3msk_to_irqnr[mask2]; } else irq = lc1msk_to_irqnr[mask]; /* if irq == 0, then the interrupt has already been cleared */ if (irq) do_IRQ(irq); } extern void ip22_be_interrupt(int irq); static void __irq_entry indy_buserror_irq(void) { int irq = SGI_BUSERR_IRQ; irq_enter(); kstat_incr_irq_this_cpu(irq); ip22_be_interrupt(irq); irq_exit(); } #ifdef USE_LIO3_IRQ #define SGI_INTERRUPTS SGINT_END #else #define SGI_INTERRUPTS SGINT_LOCAL3 #endif extern void indy_8254timer_irq(void); /* * IRQs on the INDY look basically (barring software IRQs which we don't use * at all) like: * * MIPS IRQ Source * -------- ------ * 0 Software (ignored) * 1 Software (ignored) * 2 Local IRQ level zero * 3 Local IRQ level one * 4 8254 Timer zero * 5 8254 Timer one * 6 Bus Error * 7 R4k timer (what we use) * * We handle the IRQ according to _our_ priority which is: * * Highest ---- R4k Timer * Local IRQ zero * Local IRQ one * Bus Error * 8254 Timer zero * Lowest ---- 8254 Timer one * * then we just return, if multiple IRQs are pending then we will just take * another exception, big deal. */ asmlinkage void plat_irq_dispatch(void) { unsigned int pending = read_c0_status() & read_c0_cause(); /* * First we check for r4k counter/timer IRQ. */ if (pending & CAUSEF_IP7) do_IRQ(SGI_TIMER_IRQ); else if (pending & CAUSEF_IP2) indy_local0_irqdispatch(); else if (pending & CAUSEF_IP3) indy_local1_irqdispatch(); else if (pending & CAUSEF_IP6) indy_buserror_irq(); else if (pending & (CAUSEF_IP4 | CAUSEF_IP5)) indy_8254timer_irq(); } void __init arch_init_irq(void) { int i; /* Init local mask --> irq tables. */ for (i = 0; i < 256; i++) { if (i & 0x80) { lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 7; lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 7; lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 7; lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 7; } else if (i & 0x40) { lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 6; lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 6; lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 6; lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 6; } else if (i & 0x20) { lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 5; lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 5; lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 5; lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 5; } else if (i & 0x10) { lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 4; lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 4; lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 4; lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 4; } else if (i & 0x08) { lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 3; lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 3; lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 3; lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 3; } else if (i & 0x04) { lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 2; lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 2; lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 2; lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 2; } else if (i & 0x02) { lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 1; lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 1; lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 1; lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 1; } else if (i & 0x01) { lc0msk_to_irqnr[i] = SGINT_LOCAL0 + 0; lc1msk_to_irqnr[i] = SGINT_LOCAL1 + 0; lc2msk_to_irqnr[i] = SGINT_LOCAL2 + 0; lc3msk_to_irqnr[i] = SGINT_LOCAL3 + 0; } else { lc0msk_to_irqnr[i] = 0; lc1msk_to_irqnr[i] = 0; lc2msk_to_irqnr[i] = 0; lc3msk_to_irqnr[i] = 0; } } /* Mask out all interrupts. */ sgint->imask0 = 0; sgint->imask1 = 0; sgint->cmeimask0 = 0; sgint->cmeimask1 = 0; /* init CPU irqs */ mips_cpu_irq_init(); for (i = SGINT_LOCAL0; i < SGI_INTERRUPTS; i++) { struct irq_chip *handler; if (i < SGINT_LOCAL1) handler = &ip22_local0_irq_type; else if (i < SGINT_LOCAL2) handler = &ip22_local1_irq_type; else if (i < SGINT_LOCAL3) handler = &ip22_local2_irq_type; else handler = &ip22_local3_irq_type; irq_set_chip_and_handler(i, handler, handle_level_irq); } /* vector handler. this register the IRQ as non-sharable */ if (request_irq(SGI_LOCAL_0_IRQ, no_action, IRQF_NO_THREAD, "local0 cascade", NULL)) pr_err("Failed to register local0 cascade interrupt\n"); if (request_irq(SGI_LOCAL_1_IRQ, no_action, IRQF_NO_THREAD, "local1 cascade", NULL)) pr_err("Failed to register local1 cascade interrupt\n"); if (request_irq(SGI_BUSERR_IRQ, no_action, IRQF_NO_THREAD, "Bus Error", NULL)) pr_err("Failed to register Bus Error interrupt\n"); /* cascade in cascade. i love Indy ;-) */ if (request_irq(SGI_MAP_0_IRQ, no_action, IRQF_NO_THREAD, "mapable0 cascade", NULL)) pr_err("Failed to register mapable0 cascade interrupt\n"); #ifdef USE_LIO3_IRQ if (request_irq(SGI_MAP_1_IRQ, no_action, IRQF_NO_THREAD, "mapable1 cascade", NULL)) pr_err("Failed to register mapable1 cascade interrupt\n"); #endif #ifdef CONFIG_EISA if (ip22_is_fullhouse()) /* Only Indigo-2 has EISA stuff */ ip22_eisa_init(); #endif }
linux-master
arch/mips/sgi-ip22/ip22-int.c
// SPDX-License-Identifier: GPL-2.0 /* * ip22-hpc.c: Routines for generic manipulation of the HPC controllers. * * Copyright (C) 1996 David S. Miller ([email protected]) * Copyright (C) 1998 Ralf Baechle */ #include <linux/export.h> #include <linux/init.h> #include <linux/types.h> #include <asm/io.h> #include <asm/sgi/hpc3.h> #include <asm/sgi/ioc.h> #include <asm/sgi/ip22.h> struct hpc3_regs *hpc3c0, *hpc3c1; EXPORT_SYMBOL(hpc3c0); EXPORT_SYMBOL(hpc3c1); struct sgioc_regs *sgioc; EXPORT_SYMBOL(sgioc); /* We need software copies of these because they are write only. */ u8 sgi_ioc_reset, sgi_ioc_write; extern char *system_type; void __init sgihpc_init(void) { /* ioremap can't fail */ hpc3c0 = (struct hpc3_regs *) ioremap(HPC3_CHIP0_BASE, sizeof(struct hpc3_regs)); hpc3c1 = (struct hpc3_regs *) ioremap(HPC3_CHIP1_BASE, sizeof(struct hpc3_regs)); /* IOC lives in PBUS PIO channel 6 */ sgioc = (struct sgioc_regs *)hpc3c0->pbus_extregs[6]; hpc3c0->pbus_piocfg[6][0] |= HPC3_PIOCFG_DS16; if (ip22_is_fullhouse()) { /* Full House comes with INT2 which lives in PBUS PIO * channel 4 */ sgint = (struct sgint_regs *)hpc3c0->pbus_extregs[4]; system_type = "SGI Indigo2"; } else { /* Guiness comes with INT3 which is part of IOC */ sgint = &sgioc->int3; system_type = "SGI Indy"; } sgi_ioc_reset = (SGIOC_RESET_PPORT | SGIOC_RESET_KBDMOUSE | SGIOC_RESET_EISA | SGIOC_RESET_ISDN | SGIOC_RESET_LC0OFF); sgi_ioc_write = (SGIOC_WRITE_EASEL | SGIOC_WRITE_NTHRESH | SGIOC_WRITE_TPSPEED | SGIOC_WRITE_EPSEL | SGIOC_WRITE_U0AMODE | SGIOC_WRITE_U1AMODE); sgioc->reset = sgi_ioc_reset; sgioc->write = sgi_ioc_write; }
linux-master
arch/mips/sgi-ip22/ip22-hpc.c
// SPDX-License-Identifier: GPL-2.0-only /* * Just-In-Time compiler for eBPF bytecode on MIPS. * Implementation of JIT functions for 64-bit CPUs. * * Copyright (c) 2021 Anyfi Networks AB. * Author: Johan Almbladh <[email protected]> * * Based on code and ideas from * Copyright (c) 2017 Cavium, Inc. * Copyright (c) 2017 Shubham Bansal <[email protected]> * Copyright (c) 2011 Mircea Gherzan <[email protected]> */ #include <linux/errno.h> #include <linux/filter.h> #include <linux/bpf.h> #include <asm/cpu-features.h> #include <asm/isa-rev.h> #include <asm/uasm.h> #include "bpf_jit_comp.h" /* MIPS t0-t3 are not available in the n64 ABI */ #undef MIPS_R_T0 #undef MIPS_R_T1 #undef MIPS_R_T2 #undef MIPS_R_T3 /* Stack is 16-byte aligned in n64 ABI */ #define MIPS_STACK_ALIGNMENT 16 /* Extra 64-bit eBPF registers used by JIT */ #define JIT_REG_TC (MAX_BPF_JIT_REG + 0) #define JIT_REG_ZX (MAX_BPF_JIT_REG + 1) /* Number of prologue bytes to skip when doing a tail call */ #define JIT_TCALL_SKIP 4 /* Callee-saved CPU registers that the JIT must preserve */ #define JIT_CALLEE_REGS \ (BIT(MIPS_R_S0) | \ BIT(MIPS_R_S1) | \ BIT(MIPS_R_S2) | \ BIT(MIPS_R_S3) | \ BIT(MIPS_R_S4) | \ BIT(MIPS_R_S5) | \ BIT(MIPS_R_S6) | \ BIT(MIPS_R_S7) | \ BIT(MIPS_R_GP) | \ BIT(MIPS_R_FP) | \ BIT(MIPS_R_RA)) /* Caller-saved CPU registers available for JIT use */ #define JIT_CALLER_REGS \ (BIT(MIPS_R_A5) | \ BIT(MIPS_R_A6) | \ BIT(MIPS_R_A7)) /* * Mapping of 64-bit eBPF registers to 64-bit native MIPS registers. * MIPS registers t4 - t7 may be used by the JIT as temporary registers. * MIPS registers t8 - t9 are reserved for single-register common functions. */ static const u8 bpf2mips64[] = { /* Return value from in-kernel function, and exit value from eBPF */ [BPF_REG_0] = MIPS_R_V0, /* Arguments from eBPF program to in-kernel function */ [BPF_REG_1] = MIPS_R_A0, [BPF_REG_2] = MIPS_R_A1, [BPF_REG_3] = MIPS_R_A2, [BPF_REG_4] = MIPS_R_A3, [BPF_REG_5] = MIPS_R_A4, /* Callee-saved registers that in-kernel function will preserve */ [BPF_REG_6] = MIPS_R_S0, [BPF_REG_7] = MIPS_R_S1, [BPF_REG_8] = MIPS_R_S2, [BPF_REG_9] = MIPS_R_S3, /* Read-only frame pointer to access the eBPF stack */ [BPF_REG_FP] = MIPS_R_FP, /* Temporary register for blinding constants */ [BPF_REG_AX] = MIPS_R_AT, /* Tail call count register, caller-saved */ [JIT_REG_TC] = MIPS_R_A5, /* Constant for register zero-extension */ [JIT_REG_ZX] = MIPS_R_V1, }; /* * MIPS 32-bit operations on 64-bit registers generate a sign-extended * result. However, the eBPF ISA mandates zero-extension, so we rely on the * verifier to add that for us (emit_zext_ver). In addition, ALU arithmetic * operations, right shift and byte swap require properly sign-extended * operands or the result is unpredictable. We emit explicit sign-extensions * in those cases. */ /* Sign extension */ static void emit_sext(struct jit_context *ctx, u8 dst, u8 src) { emit(ctx, sll, dst, src, 0); clobber_reg(ctx, dst); } /* Zero extension */ static void emit_zext(struct jit_context *ctx, u8 dst) { if (cpu_has_mips64r2 || cpu_has_mips64r6) { emit(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32); } else { emit(ctx, and, dst, dst, bpf2mips64[JIT_REG_ZX]); access_reg(ctx, JIT_REG_ZX); /* We need the ZX register */ } clobber_reg(ctx, dst); } /* Zero extension, if verifier does not do it for us */ static void emit_zext_ver(struct jit_context *ctx, u8 dst) { if (!ctx->program->aux->verifier_zext) emit_zext(ctx, dst); } /* dst = imm (64-bit) */ static void emit_mov_i64(struct jit_context *ctx, u8 dst, u64 imm64) { if (imm64 >= 0xffffffffffff8000ULL || imm64 < 0x8000ULL) { emit(ctx, daddiu, dst, MIPS_R_ZERO, (s16)imm64); } else if (imm64 >= 0xffffffff80000000ULL || (imm64 < 0x80000000 && imm64 > 0xffff)) { emit(ctx, lui, dst, (s16)(imm64 >> 16)); emit(ctx, ori, dst, dst, (u16)imm64 & 0xffff); } else { u8 acc = MIPS_R_ZERO; int shift = 0; int k; for (k = 0; k < 4; k++) { u16 half = imm64 >> (48 - 16 * k); if (acc == dst) shift += 16; if (half) { if (shift) emit(ctx, dsll_safe, dst, dst, shift); emit(ctx, ori, dst, acc, half); acc = dst; shift = 0; } } if (shift) emit(ctx, dsll_safe, dst, dst, shift); } clobber_reg(ctx, dst); } /* ALU immediate operation (64-bit) */ static void emit_alu_i64(struct jit_context *ctx, u8 dst, s32 imm, u8 op) { switch (BPF_OP(op)) { /* dst = dst | imm */ case BPF_OR: emit(ctx, ori, dst, dst, (u16)imm); break; /* dst = dst ^ imm */ case BPF_XOR: emit(ctx, xori, dst, dst, (u16)imm); break; /* dst = -dst */ case BPF_NEG: emit(ctx, dsubu, dst, MIPS_R_ZERO, dst); break; /* dst = dst << imm */ case BPF_LSH: emit(ctx, dsll_safe, dst, dst, imm); break; /* dst = dst >> imm */ case BPF_RSH: emit(ctx, dsrl_safe, dst, dst, imm); break; /* dst = dst >> imm (arithmetic) */ case BPF_ARSH: emit(ctx, dsra_safe, dst, dst, imm); break; /* dst = dst + imm */ case BPF_ADD: emit(ctx, daddiu, dst, dst, imm); break; /* dst = dst - imm */ case BPF_SUB: emit(ctx, daddiu, dst, dst, -imm); break; default: /* Width-generic operations */ emit_alu_i(ctx, dst, imm, op); } clobber_reg(ctx, dst); } /* ALU register operation (64-bit) */ static void emit_alu_r64(struct jit_context *ctx, u8 dst, u8 src, u8 op) { switch (BPF_OP(op)) { /* dst = dst << src */ case BPF_LSH: emit(ctx, dsllv, dst, dst, src); break; /* dst = dst >> src */ case BPF_RSH: emit(ctx, dsrlv, dst, dst, src); break; /* dst = dst >> src (arithmetic) */ case BPF_ARSH: emit(ctx, dsrav, dst, dst, src); break; /* dst = dst + src */ case BPF_ADD: emit(ctx, daddu, dst, dst, src); break; /* dst = dst - src */ case BPF_SUB: emit(ctx, dsubu, dst, dst, src); break; /* dst = dst * src */ case BPF_MUL: if (cpu_has_mips64r6) { emit(ctx, dmulu, dst, dst, src); } else { emit(ctx, dmultu, dst, src); emit(ctx, mflo, dst); /* Ensure multiplication is completed */ if (IS_ENABLED(CONFIG_CPU_R4000_WORKAROUNDS)) emit(ctx, mfhi, MIPS_R_ZERO); } break; /* dst = dst / src */ case BPF_DIV: if (cpu_has_mips64r6) { emit(ctx, ddivu_r6, dst, dst, src); } else { emit(ctx, ddivu, dst, src); emit(ctx, mflo, dst); } break; /* dst = dst % src */ case BPF_MOD: if (cpu_has_mips64r6) { emit(ctx, dmodu, dst, dst, src); } else { emit(ctx, ddivu, dst, src); emit(ctx, mfhi, dst); } break; default: /* Width-generic operations */ emit_alu_r(ctx, dst, src, op); } clobber_reg(ctx, dst); } /* Swap sub words in a register double word */ static void emit_swap_r64(struct jit_context *ctx, u8 dst, u8 mask, u32 bits) { u8 tmp = MIPS_R_T9; emit(ctx, and, tmp, dst, mask); /* tmp = dst & mask */ emit(ctx, dsll, tmp, tmp, bits); /* tmp = tmp << bits */ emit(ctx, dsrl, dst, dst, bits); /* dst = dst >> bits */ emit(ctx, and, dst, dst, mask); /* dst = dst & mask */ emit(ctx, or, dst, dst, tmp); /* dst = dst | tmp */ } /* Swap bytes and truncate a register double word, word or half word */ static void emit_bswap_r64(struct jit_context *ctx, u8 dst, u32 width) { switch (width) { /* Swap bytes in a double word */ case 64: if (cpu_has_mips64r2 || cpu_has_mips64r6) { emit(ctx, dsbh, dst, dst); emit(ctx, dshd, dst, dst); } else { u8 t1 = MIPS_R_T6; u8 t2 = MIPS_R_T7; emit(ctx, dsll32, t2, dst, 0); /* t2 = dst << 32 */ emit(ctx, dsrl32, dst, dst, 0); /* dst = dst >> 32 */ emit(ctx, or, dst, dst, t2); /* dst = dst | t2 */ emit(ctx, ori, t2, MIPS_R_ZERO, 0xffff); emit(ctx, dsll32, t1, t2, 0); /* t1 = t2 << 32 */ emit(ctx, or, t1, t1, t2); /* t1 = t1 | t2 */ emit_swap_r64(ctx, dst, t1, 16);/* dst = swap16(dst) */ emit(ctx, lui, t2, 0xff); /* t2 = 0x00ff0000 */ emit(ctx, ori, t2, t2, 0xff); /* t2 = t2 | 0x00ff */ emit(ctx, dsll32, t1, t2, 0); /* t1 = t2 << 32 */ emit(ctx, or, t1, t1, t2); /* t1 = t1 | t2 */ emit_swap_r64(ctx, dst, t1, 8); /* dst = swap8(dst) */ } break; /* Swap bytes in a half word */ /* Swap bytes in a word */ case 32: case 16: emit_sext(ctx, dst, dst); emit_bswap_r(ctx, dst, width); if (cpu_has_mips64r2 || cpu_has_mips64r6) emit_zext(ctx, dst); break; } clobber_reg(ctx, dst); } /* Truncate a register double word, word or half word */ static void emit_trunc_r64(struct jit_context *ctx, u8 dst, u32 width) { switch (width) { case 64: break; /* Zero-extend a word */ case 32: emit_zext(ctx, dst); break; /* Zero-extend a half word */ case 16: emit(ctx, andi, dst, dst, 0xffff); break; } clobber_reg(ctx, dst); } /* Load operation: dst = *(size*)(src + off) */ static void emit_ldx(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 size) { switch (size) { /* Load a byte */ case BPF_B: emit(ctx, lbu, dst, off, src); break; /* Load a half word */ case BPF_H: emit(ctx, lhu, dst, off, src); break; /* Load a word */ case BPF_W: emit(ctx, lwu, dst, off, src); break; /* Load a double word */ case BPF_DW: emit(ctx, ld, dst, off, src); break; } clobber_reg(ctx, dst); } /* Store operation: *(size *)(dst + off) = src */ static void emit_stx(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 size) { switch (size) { /* Store a byte */ case BPF_B: emit(ctx, sb, src, off, dst); break; /* Store a half word */ case BPF_H: emit(ctx, sh, src, off, dst); break; /* Store a word */ case BPF_W: emit(ctx, sw, src, off, dst); break; /* Store a double word */ case BPF_DW: emit(ctx, sd, src, off, dst); break; } } /* Atomic read-modify-write */ static void emit_atomic_r64(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code) { u8 t1 = MIPS_R_T6; u8 t2 = MIPS_R_T7; LLSC_sync(ctx); emit(ctx, lld, t1, off, dst); switch (code) { case BPF_ADD: case BPF_ADD | BPF_FETCH: emit(ctx, daddu, t2, t1, src); break; case BPF_AND: case BPF_AND | BPF_FETCH: emit(ctx, and, t2, t1, src); break; case BPF_OR: case BPF_OR | BPF_FETCH: emit(ctx, or, t2, t1, src); break; case BPF_XOR: case BPF_XOR | BPF_FETCH: emit(ctx, xor, t2, t1, src); break; case BPF_XCHG: emit(ctx, move, t2, src); break; } emit(ctx, scd, t2, off, dst); emit(ctx, LLSC_beqz, t2, -16 - LLSC_offset); emit(ctx, nop); /* Delay slot */ if (code & BPF_FETCH) { emit(ctx, move, src, t1); clobber_reg(ctx, src); } } /* Atomic compare-and-exchange */ static void emit_cmpxchg_r64(struct jit_context *ctx, u8 dst, u8 src, s16 off) { u8 r0 = bpf2mips64[BPF_REG_0]; u8 t1 = MIPS_R_T6; u8 t2 = MIPS_R_T7; LLSC_sync(ctx); emit(ctx, lld, t1, off, dst); emit(ctx, bne, t1, r0, 12); emit(ctx, move, t2, src); /* Delay slot */ emit(ctx, scd, t2, off, dst); emit(ctx, LLSC_beqz, t2, -20 - LLSC_offset); emit(ctx, move, r0, t1); /* Delay slot */ clobber_reg(ctx, r0); } /* Function call */ static int emit_call(struct jit_context *ctx, const struct bpf_insn *insn) { u8 zx = bpf2mips64[JIT_REG_ZX]; u8 tmp = MIPS_R_T6; bool fixed; u64 addr; /* Decode the call address */ if (bpf_jit_get_func_addr(ctx->program, insn, false, &addr, &fixed) < 0) return -1; if (!fixed) return -1; /* Push caller-saved registers on stack */ push_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, 0, 0); /* Emit function call */ emit_mov_i64(ctx, tmp, addr & JALR_MASK); emit(ctx, jalr, MIPS_R_RA, tmp); emit(ctx, nop); /* Delay slot */ /* Restore caller-saved registers */ pop_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, 0, 0); /* Re-initialize the JIT zero-extension register if accessed */ if (ctx->accessed & BIT(JIT_REG_ZX)) { emit(ctx, daddiu, zx, MIPS_R_ZERO, -1); emit(ctx, dsrl32, zx, zx, 0); } clobber_reg(ctx, MIPS_R_RA); clobber_reg(ctx, MIPS_R_V0); clobber_reg(ctx, MIPS_R_V1); return 0; } /* Function tail call */ static int emit_tail_call(struct jit_context *ctx) { u8 ary = bpf2mips64[BPF_REG_2]; u8 ind = bpf2mips64[BPF_REG_3]; u8 tcc = bpf2mips64[JIT_REG_TC]; u8 tmp = MIPS_R_T6; int off; /* * Tail call: * eBPF R1 - function argument (context ptr), passed in a0-a1 * eBPF R2 - ptr to object with array of function entry points * eBPF R3 - array index of function to be called */ /* if (ind >= ary->map.max_entries) goto out */ off = offsetof(struct bpf_array, map.max_entries); if (off > 0x7fff) return -1; emit(ctx, lwu, tmp, off, ary); /* tmp = ary->map.max_entrs*/ emit(ctx, sltu, tmp, ind, tmp); /* tmp = ind < t1 */ emit(ctx, beqz, tmp, get_offset(ctx, 1)); /* PC += off(1) if tmp == 0*/ /* if (--TCC < 0) goto out */ emit(ctx, daddiu, tcc, tcc, -1); /* tcc-- (delay slot) */ emit(ctx, bltz, tcc, get_offset(ctx, 1)); /* PC += off(1) if tcc < 0 */ /* (next insn delay slot) */ /* prog = ary->ptrs[ind] */ off = offsetof(struct bpf_array, ptrs); if (off > 0x7fff) return -1; emit(ctx, dsll, tmp, ind, 3); /* tmp = ind << 3 */ emit(ctx, daddu, tmp, tmp, ary); /* tmp += ary */ emit(ctx, ld, tmp, off, tmp); /* tmp = *(tmp + off) */ /* if (prog == 0) goto out */ emit(ctx, beqz, tmp, get_offset(ctx, 1)); /* PC += off(1) if tmp == 0*/ emit(ctx, nop); /* Delay slot */ /* func = prog->bpf_func + 8 (prologue skip offset) */ off = offsetof(struct bpf_prog, bpf_func); if (off > 0x7fff) return -1; emit(ctx, ld, tmp, off, tmp); /* tmp = *(tmp + off) */ emit(ctx, daddiu, tmp, tmp, JIT_TCALL_SKIP); /* tmp += skip (4) */ /* goto func */ build_epilogue(ctx, tmp); access_reg(ctx, JIT_REG_TC); return 0; } /* * Stack frame layout for a JITed program (stack grows down). * * Higher address : Previous stack frame : * +===========================+ <--- MIPS sp before call * | Callee-saved registers, | * | including RA and FP | * +---------------------------+ <--- eBPF FP (MIPS fp) * | Local eBPF variables | * | allocated by program | * +---------------------------+ * | Reserved for caller-saved | * | registers | * Lower address +===========================+ <--- MIPS sp */ /* Build program prologue to set up the stack and registers */ void build_prologue(struct jit_context *ctx) { u8 fp = bpf2mips64[BPF_REG_FP]; u8 tc = bpf2mips64[JIT_REG_TC]; u8 zx = bpf2mips64[JIT_REG_ZX]; int stack, saved, locals, reserved; /* * In the unlikely event that the TCC limit is raised to more * than 16 bits, it is clamped to the maximum value allowed for * the generated code (0xffff). It is better fail to compile * instead of degrading gracefully. */ BUILD_BUG_ON(MAX_TAIL_CALL_CNT > 0xffff); /* * The first instruction initializes the tail call count register. * On a tail call, the calling function jumps into the prologue * after this instruction. */ emit(ctx, ori, tc, MIPS_R_ZERO, MAX_TAIL_CALL_CNT); /* === Entry-point for tail calls === */ /* * If the eBPF frame pointer and tail call count registers were * accessed they must be preserved. Mark them as clobbered here * to save and restore them on the stack as needed. */ if (ctx->accessed & BIT(BPF_REG_FP)) clobber_reg(ctx, fp); if (ctx->accessed & BIT(JIT_REG_TC)) clobber_reg(ctx, tc); if (ctx->accessed & BIT(JIT_REG_ZX)) clobber_reg(ctx, zx); /* Compute the stack space needed for callee-saved registers */ saved = hweight32(ctx->clobbered & JIT_CALLEE_REGS) * sizeof(u64); saved = ALIGN(saved, MIPS_STACK_ALIGNMENT); /* Stack space used by eBPF program local data */ locals = ALIGN(ctx->program->aux->stack_depth, MIPS_STACK_ALIGNMENT); /* * If we are emitting function calls, reserve extra stack space for * caller-saved registers needed by the JIT. The required space is * computed automatically during resource usage discovery (pass 1). */ reserved = ctx->stack_used; /* Allocate the stack frame */ stack = ALIGN(saved + locals + reserved, MIPS_STACK_ALIGNMENT); if (stack) emit(ctx, daddiu, MIPS_R_SP, MIPS_R_SP, -stack); /* Store callee-saved registers on stack */ push_regs(ctx, ctx->clobbered & JIT_CALLEE_REGS, 0, stack - saved); /* Initialize the eBPF frame pointer if accessed */ if (ctx->accessed & BIT(BPF_REG_FP)) emit(ctx, daddiu, fp, MIPS_R_SP, stack - saved); /* Initialize the ePF JIT zero-extension register if accessed */ if (ctx->accessed & BIT(JIT_REG_ZX)) { emit(ctx, daddiu, zx, MIPS_R_ZERO, -1); emit(ctx, dsrl32, zx, zx, 0); } ctx->saved_size = saved; ctx->stack_size = stack; } /* Build the program epilogue to restore the stack and registers */ void build_epilogue(struct jit_context *ctx, int dest_reg) { /* Restore callee-saved registers from stack */ pop_regs(ctx, ctx->clobbered & JIT_CALLEE_REGS, 0, ctx->stack_size - ctx->saved_size); /* Release the stack frame */ if (ctx->stack_size) emit(ctx, daddiu, MIPS_R_SP, MIPS_R_SP, ctx->stack_size); /* Jump to return address and sign-extend the 32-bit return value */ emit(ctx, jr, dest_reg); emit(ctx, sll, MIPS_R_V0, MIPS_R_V0, 0); /* Delay slot */ } /* Build one eBPF instruction */ int build_insn(const struct bpf_insn *insn, struct jit_context *ctx) { u8 dst = bpf2mips64[insn->dst_reg]; u8 src = bpf2mips64[insn->src_reg]; u8 res = bpf2mips64[BPF_REG_0]; u8 code = insn->code; s16 off = insn->off; s32 imm = insn->imm; s32 val, rel; u8 alu, jmp; switch (code) { /* ALU operations */ /* dst = imm */ case BPF_ALU | BPF_MOV | BPF_K: emit_mov_i(ctx, dst, imm); emit_zext_ver(ctx, dst); break; /* dst = src */ case BPF_ALU | BPF_MOV | BPF_X: if (imm == 1) { /* Special mov32 for zext */ emit_zext(ctx, dst); } else { emit_mov_r(ctx, dst, src); emit_zext_ver(ctx, dst); } break; /* dst = -dst */ case BPF_ALU | BPF_NEG: emit_sext(ctx, dst, dst); emit_alu_i(ctx, dst, 0, BPF_NEG); emit_zext_ver(ctx, dst); break; /* dst = dst & imm */ /* dst = dst | imm */ /* dst = dst ^ imm */ /* dst = dst << imm */ case BPF_ALU | BPF_OR | BPF_K: case BPF_ALU | BPF_AND | BPF_K: case BPF_ALU | BPF_XOR | BPF_K: case BPF_ALU | BPF_LSH | BPF_K: if (!valid_alu_i(BPF_OP(code), imm)) { emit_mov_i(ctx, MIPS_R_T4, imm); emit_alu_r(ctx, dst, MIPS_R_T4, BPF_OP(code)); } else if (rewrite_alu_i(BPF_OP(code), imm, &alu, &val)) { emit_alu_i(ctx, dst, val, alu); } emit_zext_ver(ctx, dst); break; /* dst = dst >> imm */ /* dst = dst >> imm (arithmetic) */ /* dst = dst + imm */ /* dst = dst - imm */ /* dst = dst * imm */ /* dst = dst / imm */ /* dst = dst % imm */ case BPF_ALU | BPF_RSH | BPF_K: case BPF_ALU | BPF_ARSH | BPF_K: case BPF_ALU | BPF_ADD | BPF_K: case BPF_ALU | BPF_SUB | BPF_K: case BPF_ALU | BPF_MUL | BPF_K: case BPF_ALU | BPF_DIV | BPF_K: case BPF_ALU | BPF_MOD | BPF_K: if (!valid_alu_i(BPF_OP(code), imm)) { emit_sext(ctx, dst, dst); emit_mov_i(ctx, MIPS_R_T4, imm); emit_alu_r(ctx, dst, MIPS_R_T4, BPF_OP(code)); } else if (rewrite_alu_i(BPF_OP(code), imm, &alu, &val)) { emit_sext(ctx, dst, dst); emit_alu_i(ctx, dst, val, alu); } emit_zext_ver(ctx, dst); break; /* dst = dst & src */ /* dst = dst | src */ /* dst = dst ^ src */ /* dst = dst << src */ case BPF_ALU | BPF_AND | BPF_X: case BPF_ALU | BPF_OR | BPF_X: case BPF_ALU | BPF_XOR | BPF_X: case BPF_ALU | BPF_LSH | BPF_X: emit_alu_r(ctx, dst, src, BPF_OP(code)); emit_zext_ver(ctx, dst); break; /* dst = dst >> src */ /* dst = dst >> src (arithmetic) */ /* dst = dst + src */ /* dst = dst - src */ /* dst = dst * src */ /* dst = dst / src */ /* dst = dst % src */ case BPF_ALU | BPF_RSH | BPF_X: case BPF_ALU | BPF_ARSH | BPF_X: case BPF_ALU | BPF_ADD | BPF_X: case BPF_ALU | BPF_SUB | BPF_X: case BPF_ALU | BPF_MUL | BPF_X: case BPF_ALU | BPF_DIV | BPF_X: case BPF_ALU | BPF_MOD | BPF_X: emit_sext(ctx, dst, dst); emit_sext(ctx, MIPS_R_T4, src); emit_alu_r(ctx, dst, MIPS_R_T4, BPF_OP(code)); emit_zext_ver(ctx, dst); break; /* dst = imm (64-bit) */ case BPF_ALU64 | BPF_MOV | BPF_K: emit_mov_i(ctx, dst, imm); break; /* dst = src (64-bit) */ case BPF_ALU64 | BPF_MOV | BPF_X: emit_mov_r(ctx, dst, src); break; /* dst = -dst (64-bit) */ case BPF_ALU64 | BPF_NEG: emit_alu_i64(ctx, dst, 0, BPF_NEG); break; /* dst = dst & imm (64-bit) */ /* dst = dst | imm (64-bit) */ /* dst = dst ^ imm (64-bit) */ /* dst = dst << imm (64-bit) */ /* dst = dst >> imm (64-bit) */ /* dst = dst >> imm ((64-bit, arithmetic) */ /* dst = dst + imm (64-bit) */ /* dst = dst - imm (64-bit) */ /* dst = dst * imm (64-bit) */ /* dst = dst / imm (64-bit) */ /* dst = dst % imm (64-bit) */ case BPF_ALU64 | BPF_AND | BPF_K: case BPF_ALU64 | BPF_OR | BPF_K: case BPF_ALU64 | BPF_XOR | BPF_K: case BPF_ALU64 | BPF_LSH | BPF_K: case BPF_ALU64 | BPF_RSH | BPF_K: case BPF_ALU64 | BPF_ARSH | BPF_K: case BPF_ALU64 | BPF_ADD | BPF_K: case BPF_ALU64 | BPF_SUB | BPF_K: case BPF_ALU64 | BPF_MUL | BPF_K: case BPF_ALU64 | BPF_DIV | BPF_K: case BPF_ALU64 | BPF_MOD | BPF_K: if (!valid_alu_i(BPF_OP(code), imm)) { emit_mov_i(ctx, MIPS_R_T4, imm); emit_alu_r64(ctx, dst, MIPS_R_T4, BPF_OP(code)); } else if (rewrite_alu_i(BPF_OP(code), imm, &alu, &val)) { emit_alu_i64(ctx, dst, val, alu); } break; /* dst = dst & src (64-bit) */ /* dst = dst | src (64-bit) */ /* dst = dst ^ src (64-bit) */ /* dst = dst << src (64-bit) */ /* dst = dst >> src (64-bit) */ /* dst = dst >> src (64-bit, arithmetic) */ /* dst = dst + src (64-bit) */ /* dst = dst - src (64-bit) */ /* dst = dst * src (64-bit) */ /* dst = dst / src (64-bit) */ /* dst = dst % src (64-bit) */ case BPF_ALU64 | BPF_AND | BPF_X: case BPF_ALU64 | BPF_OR | BPF_X: case BPF_ALU64 | BPF_XOR | BPF_X: case BPF_ALU64 | BPF_LSH | BPF_X: case BPF_ALU64 | BPF_RSH | BPF_X: case BPF_ALU64 | BPF_ARSH | BPF_X: case BPF_ALU64 | BPF_ADD | BPF_X: case BPF_ALU64 | BPF_SUB | BPF_X: case BPF_ALU64 | BPF_MUL | BPF_X: case BPF_ALU64 | BPF_DIV | BPF_X: case BPF_ALU64 | BPF_MOD | BPF_X: emit_alu_r64(ctx, dst, src, BPF_OP(code)); break; /* dst = htole(dst) */ /* dst = htobe(dst) */ case BPF_ALU | BPF_END | BPF_FROM_LE: case BPF_ALU | BPF_END | BPF_FROM_BE: if (BPF_SRC(code) == #ifdef __BIG_ENDIAN BPF_FROM_LE #else BPF_FROM_BE #endif ) emit_bswap_r64(ctx, dst, imm); else emit_trunc_r64(ctx, dst, imm); break; /* dst = imm64 */ case BPF_LD | BPF_IMM | BPF_DW: emit_mov_i64(ctx, dst, (u32)imm | ((u64)insn[1].imm << 32)); return 1; /* LDX: dst = *(size *)(src + off) */ case BPF_LDX | BPF_MEM | BPF_W: case BPF_LDX | BPF_MEM | BPF_H: case BPF_LDX | BPF_MEM | BPF_B: case BPF_LDX | BPF_MEM | BPF_DW: emit_ldx(ctx, dst, src, off, BPF_SIZE(code)); break; /* ST: *(size *)(dst + off) = imm */ case BPF_ST | BPF_MEM | BPF_W: case BPF_ST | BPF_MEM | BPF_H: case BPF_ST | BPF_MEM | BPF_B: case BPF_ST | BPF_MEM | BPF_DW: emit_mov_i(ctx, MIPS_R_T4, imm); emit_stx(ctx, dst, MIPS_R_T4, off, BPF_SIZE(code)); break; /* STX: *(size *)(dst + off) = src */ case BPF_STX | BPF_MEM | BPF_W: case BPF_STX | BPF_MEM | BPF_H: case BPF_STX | BPF_MEM | BPF_B: case BPF_STX | BPF_MEM | BPF_DW: emit_stx(ctx, dst, src, off, BPF_SIZE(code)); break; /* Speculation barrier */ case BPF_ST | BPF_NOSPEC: break; /* Atomics */ case BPF_STX | BPF_ATOMIC | BPF_W: case BPF_STX | BPF_ATOMIC | BPF_DW: switch (imm) { case BPF_ADD: case BPF_ADD | BPF_FETCH: case BPF_AND: case BPF_AND | BPF_FETCH: case BPF_OR: case BPF_OR | BPF_FETCH: case BPF_XOR: case BPF_XOR | BPF_FETCH: case BPF_XCHG: if (BPF_SIZE(code) == BPF_DW) { emit_atomic_r64(ctx, dst, src, off, imm); } else if (imm & BPF_FETCH) { u8 tmp = dst; if (src == dst) { /* Don't overwrite dst */ emit_mov_r(ctx, MIPS_R_T4, dst); tmp = MIPS_R_T4; } emit_sext(ctx, src, src); emit_atomic_r(ctx, tmp, src, off, imm); emit_zext_ver(ctx, src); } else { /* 32-bit, no fetch */ emit_sext(ctx, MIPS_R_T4, src); emit_atomic_r(ctx, dst, MIPS_R_T4, off, imm); } break; case BPF_CMPXCHG: if (BPF_SIZE(code) == BPF_DW) { emit_cmpxchg_r64(ctx, dst, src, off); } else { u8 tmp = res; if (res == dst) /* Don't overwrite dst */ tmp = MIPS_R_T4; emit_sext(ctx, tmp, res); emit_sext(ctx, MIPS_R_T5, src); emit_cmpxchg_r(ctx, dst, MIPS_R_T5, tmp, off); if (res == dst) /* Restore result */ emit_mov_r(ctx, res, MIPS_R_T4); /* Result zext inserted by verifier */ } break; default: goto notyet; } break; /* PC += off if dst == src */ /* PC += off if dst != src */ /* PC += off if dst & src */ /* PC += off if dst > src */ /* PC += off if dst >= src */ /* PC += off if dst < src */ /* PC += off if dst <= src */ /* PC += off if dst > src (signed) */ /* PC += off if dst >= src (signed) */ /* PC += off if dst < src (signed) */ /* PC += off if dst <= src (signed) */ case BPF_JMP32 | BPF_JEQ | BPF_X: case BPF_JMP32 | BPF_JNE | BPF_X: case BPF_JMP32 | BPF_JSET | BPF_X: case BPF_JMP32 | BPF_JGT | BPF_X: case BPF_JMP32 | BPF_JGE | BPF_X: case BPF_JMP32 | BPF_JLT | BPF_X: case BPF_JMP32 | BPF_JLE | BPF_X: case BPF_JMP32 | BPF_JSGT | BPF_X: case BPF_JMP32 | BPF_JSGE | BPF_X: case BPF_JMP32 | BPF_JSLT | BPF_X: case BPF_JMP32 | BPF_JSLE | BPF_X: if (off == 0) break; setup_jmp_r(ctx, dst == src, BPF_OP(code), off, &jmp, &rel); emit_sext(ctx, MIPS_R_T4, dst); /* Sign-extended dst */ emit_sext(ctx, MIPS_R_T5, src); /* Sign-extended src */ emit_jmp_r(ctx, MIPS_R_T4, MIPS_R_T5, rel, jmp); if (finish_jmp(ctx, jmp, off) < 0) goto toofar; break; /* PC += off if dst == imm */ /* PC += off if dst != imm */ /* PC += off if dst & imm */ /* PC += off if dst > imm */ /* PC += off if dst >= imm */ /* PC += off if dst < imm */ /* PC += off if dst <= imm */ /* PC += off if dst > imm (signed) */ /* PC += off if dst >= imm (signed) */ /* PC += off if dst < imm (signed) */ /* PC += off if dst <= imm (signed) */ case BPF_JMP32 | BPF_JEQ | BPF_K: case BPF_JMP32 | BPF_JNE | BPF_K: case BPF_JMP32 | BPF_JSET | BPF_K: case BPF_JMP32 | BPF_JGT | BPF_K: case BPF_JMP32 | BPF_JGE | BPF_K: case BPF_JMP32 | BPF_JLT | BPF_K: case BPF_JMP32 | BPF_JLE | BPF_K: case BPF_JMP32 | BPF_JSGT | BPF_K: case BPF_JMP32 | BPF_JSGE | BPF_K: case BPF_JMP32 | BPF_JSLT | BPF_K: case BPF_JMP32 | BPF_JSLE | BPF_K: if (off == 0) break; setup_jmp_i(ctx, imm, 32, BPF_OP(code), off, &jmp, &rel); emit_sext(ctx, MIPS_R_T4, dst); /* Sign-extended dst */ if (valid_jmp_i(jmp, imm)) { emit_jmp_i(ctx, MIPS_R_T4, imm, rel, jmp); } else { /* Move large immediate to register, sign-extended */ emit_mov_i(ctx, MIPS_R_T5, imm); emit_jmp_r(ctx, MIPS_R_T4, MIPS_R_T5, rel, jmp); } if (finish_jmp(ctx, jmp, off) < 0) goto toofar; break; /* PC += off if dst == src */ /* PC += off if dst != src */ /* PC += off if dst & src */ /* PC += off if dst > src */ /* PC += off if dst >= src */ /* PC += off if dst < src */ /* PC += off if dst <= src */ /* PC += off if dst > src (signed) */ /* PC += off if dst >= src (signed) */ /* PC += off if dst < src (signed) */ /* PC += off if dst <= src (signed) */ case BPF_JMP | BPF_JEQ | BPF_X: case BPF_JMP | BPF_JNE | BPF_X: case BPF_JMP | BPF_JSET | BPF_X: case BPF_JMP | BPF_JGT | BPF_X: case BPF_JMP | BPF_JGE | BPF_X: case BPF_JMP | BPF_JLT | BPF_X: case BPF_JMP | BPF_JLE | BPF_X: case BPF_JMP | BPF_JSGT | BPF_X: case BPF_JMP | BPF_JSGE | BPF_X: case BPF_JMP | BPF_JSLT | BPF_X: case BPF_JMP | BPF_JSLE | BPF_X: if (off == 0) break; setup_jmp_r(ctx, dst == src, BPF_OP(code), off, &jmp, &rel); emit_jmp_r(ctx, dst, src, rel, jmp); if (finish_jmp(ctx, jmp, off) < 0) goto toofar; break; /* PC += off if dst == imm */ /* PC += off if dst != imm */ /* PC += off if dst & imm */ /* PC += off if dst > imm */ /* PC += off if dst >= imm */ /* PC += off if dst < imm */ /* PC += off if dst <= imm */ /* PC += off if dst > imm (signed) */ /* PC += off if dst >= imm (signed) */ /* PC += off if dst < imm (signed) */ /* PC += off if dst <= imm (signed) */ case BPF_JMP | BPF_JEQ | BPF_K: case BPF_JMP | BPF_JNE | BPF_K: case BPF_JMP | BPF_JSET | BPF_K: case BPF_JMP | BPF_JGT | BPF_K: case BPF_JMP | BPF_JGE | BPF_K: case BPF_JMP | BPF_JLT | BPF_K: case BPF_JMP | BPF_JLE | BPF_K: case BPF_JMP | BPF_JSGT | BPF_K: case BPF_JMP | BPF_JSGE | BPF_K: case BPF_JMP | BPF_JSLT | BPF_K: case BPF_JMP | BPF_JSLE | BPF_K: if (off == 0) break; setup_jmp_i(ctx, imm, 64, BPF_OP(code), off, &jmp, &rel); if (valid_jmp_i(jmp, imm)) { emit_jmp_i(ctx, dst, imm, rel, jmp); } else { /* Move large immediate to register */ emit_mov_i(ctx, MIPS_R_T4, imm); emit_jmp_r(ctx, dst, MIPS_R_T4, rel, jmp); } if (finish_jmp(ctx, jmp, off) < 0) goto toofar; break; /* PC += off */ case BPF_JMP | BPF_JA: if (off == 0) break; if (emit_ja(ctx, off) < 0) goto toofar; break; /* Tail call */ case BPF_JMP | BPF_TAIL_CALL: if (emit_tail_call(ctx) < 0) goto invalid; break; /* Function call */ case BPF_JMP | BPF_CALL: if (emit_call(ctx, insn) < 0) goto invalid; break; /* Function return */ case BPF_JMP | BPF_EXIT: /* * Optimization: when last instruction is EXIT * simply continue to epilogue. */ if (ctx->bpf_index == ctx->program->len - 1) break; if (emit_exit(ctx) < 0) goto toofar; break; default: invalid: pr_err_once("unknown opcode %02x\n", code); return -EINVAL; notyet: pr_info_once("*** NOT YET: opcode %02x ***\n", code); return -EFAULT; toofar: pr_info_once("*** TOO FAR: jump at %u opcode %02x ***\n", ctx->bpf_index, code); return -E2BIG; } return 0; }
linux-master
arch/mips/net/bpf_jit_comp64.c
// SPDX-License-Identifier: GPL-2.0-only /* * Just-In-Time compiler for eBPF bytecode on MIPS. * Implementation of JIT functions common to 32-bit and 64-bit CPUs. * * Copyright (c) 2021 Anyfi Networks AB. * Author: Johan Almbladh <[email protected]> * * Based on code and ideas from * Copyright (c) 2017 Cavium, Inc. * Copyright (c) 2017 Shubham Bansal <[email protected]> * Copyright (c) 2011 Mircea Gherzan <[email protected]> */ /* * Code overview * ============= * * - bpf_jit_comp.h * Common definitions and utilities. * * - bpf_jit_comp.c * Implementation of JIT top-level logic and exported JIT API functions. * Implementation of internal operations shared by 32-bit and 64-bit code. * JMP and ALU JIT control code, register control code, shared ALU and * JMP/JMP32 JIT operations. * * - bpf_jit_comp32.c * Implementation of functions to JIT prologue, epilogue and a single eBPF * instruction for 32-bit MIPS CPUs. The functions use shared operations * where possible, and implement the rest for 32-bit MIPS such as ALU64 * operations. * * - bpf_jit_comp64.c * Ditto, for 64-bit MIPS CPUs. * * Zero and sign extension * ======================== * 32-bit MIPS instructions on 64-bit MIPS registers use sign extension, * but the eBPF instruction set mandates zero extension. We let the verifier * insert explicit zero-extensions after 32-bit ALU operations, both for * 32-bit and 64-bit MIPS JITs. Conditional JMP32 operations on 64-bit MIPs * are JITed with sign extensions inserted when so expected. * * ALU operations * ============== * ALU operations on 32/64-bit MIPS and ALU64 operations on 64-bit MIPS are * JITed in the following steps. ALU64 operations on 32-bit MIPS are more * complicated and therefore only processed by special implementations in * step (3). * * 1) valid_alu_i: * Determine if an immediate operation can be emitted as such, or if * we must fall back to the register version. * * 2) rewrite_alu_i: * Convert BPF operation and immediate value to a canonical form for * JITing. In some degenerate cases this form may be a no-op. * * 3) emit_alu_{i,i64,r,64}: * Emit instructions for an ALU or ALU64 immediate or register operation. * * JMP operations * ============== * JMP and JMP32 operations require an JIT instruction offset table for * translating the jump offset. This table is computed by dry-running the * JIT without actually emitting anything. However, the computed PC-relative * offset may overflow the 18-bit offset field width of the native MIPS * branch instruction. In such cases, the long jump is converted into the * following sequence. * * <branch> !<cond> +2 Inverted PC-relative branch * nop Delay slot * j <offset> Unconditional absolute long jump * nop Delay slot * * Since this converted sequence alters the offset table, all offsets must * be re-calculated. This may in turn trigger new branch conversions, so * the process is repeated until no further changes are made. Normally it * completes in 1-2 iterations. If JIT_MAX_ITERATIONS should reached, we * fall back to converting every remaining jump operation. The branch * conversion is independent of how the JMP or JMP32 condition is JITed. * * JMP32 and JMP operations are JITed as follows. * * 1) setup_jmp_{i,r}: * Convert jump conditional and offset into a form that can be JITed. * This form may be a no-op, a canonical form, or an inverted PC-relative * jump if branch conversion is necessary. * * 2) valid_jmp_i: * Determine if an immediate operations can be emitted as such, or if * we must fall back to the register version. Applies to JMP32 for 32-bit * MIPS, and both JMP and JMP32 for 64-bit MIPS. * * 3) emit_jmp_{i,i64,r,r64}: * Emit instructions for an JMP or JMP32 immediate or register operation. * * 4) finish_jmp_{i,r}: * Emit any instructions needed to finish the jump. This includes a nop * for the delay slot if a branch was emitted, and a long absolute jump * if the branch was converted. */ #include <linux/limits.h> #include <linux/bitops.h> #include <linux/errno.h> #include <linux/filter.h> #include <linux/bpf.h> #include <linux/slab.h> #include <asm/bitops.h> #include <asm/cacheflush.h> #include <asm/cpu-features.h> #include <asm/isa-rev.h> #include <asm/uasm.h> #include "bpf_jit_comp.h" /* Convenience macros for descriptor access */ #define CONVERTED(desc) ((desc) & JIT_DESC_CONVERT) #define INDEX(desc) ((desc) & ~JIT_DESC_CONVERT) /* * Push registers on the stack, starting at a given depth from the stack * pointer and increasing. The next depth to be written is returned. */ int push_regs(struct jit_context *ctx, u32 mask, u32 excl, int depth) { int reg; for (reg = 0; reg < BITS_PER_BYTE * sizeof(mask); reg++) if (mask & BIT(reg)) { if ((excl & BIT(reg)) == 0) { if (sizeof(long) == 4) emit(ctx, sw, reg, depth, MIPS_R_SP); else /* sizeof(long) == 8 */ emit(ctx, sd, reg, depth, MIPS_R_SP); } depth += sizeof(long); } ctx->stack_used = max((int)ctx->stack_used, depth); return depth; } /* * Pop registers from the stack, starting at a given depth from the stack * pointer and increasing. The next depth to be read is returned. */ int pop_regs(struct jit_context *ctx, u32 mask, u32 excl, int depth) { int reg; for (reg = 0; reg < BITS_PER_BYTE * sizeof(mask); reg++) if (mask & BIT(reg)) { if ((excl & BIT(reg)) == 0) { if (sizeof(long) == 4) emit(ctx, lw, reg, depth, MIPS_R_SP); else /* sizeof(long) == 8 */ emit(ctx, ld, reg, depth, MIPS_R_SP); } depth += sizeof(long); } return depth; } /* Compute the 28-bit jump target address from a BPF program location */ int get_target(struct jit_context *ctx, u32 loc) { u32 index = INDEX(ctx->descriptors[loc]); unsigned long pc = (unsigned long)&ctx->target[ctx->jit_index]; unsigned long addr = (unsigned long)&ctx->target[index]; if (!ctx->target) return 0; if ((addr ^ pc) & ~MIPS_JMP_MASK) return -1; return addr & MIPS_JMP_MASK; } /* Compute the PC-relative offset to relative BPF program offset */ int get_offset(const struct jit_context *ctx, int off) { return (INDEX(ctx->descriptors[ctx->bpf_index + off]) - ctx->jit_index - 1) * sizeof(u32); } /* dst = imm (register width) */ void emit_mov_i(struct jit_context *ctx, u8 dst, s32 imm) { if (imm >= -0x8000 && imm <= 0x7fff) { emit(ctx, addiu, dst, MIPS_R_ZERO, imm); } else { emit(ctx, lui, dst, (s16)((u32)imm >> 16)); emit(ctx, ori, dst, dst, (u16)(imm & 0xffff)); } clobber_reg(ctx, dst); } /* dst = src (register width) */ void emit_mov_r(struct jit_context *ctx, u8 dst, u8 src) { emit(ctx, ori, dst, src, 0); clobber_reg(ctx, dst); } /* Validate ALU immediate range */ bool valid_alu_i(u8 op, s32 imm) { switch (BPF_OP(op)) { case BPF_NEG: case BPF_LSH: case BPF_RSH: case BPF_ARSH: /* All legal eBPF values are valid */ return true; case BPF_ADD: if (IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS)) return false; /* imm must be 16 bits */ return imm >= -0x8000 && imm <= 0x7fff; case BPF_SUB: if (IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS)) return false; /* -imm must be 16 bits */ return imm >= -0x7fff && imm <= 0x8000; case BPF_AND: case BPF_OR: case BPF_XOR: /* imm must be 16 bits unsigned */ return imm >= 0 && imm <= 0xffff; case BPF_MUL: /* imm must be zero or a positive power of two */ return imm == 0 || (imm > 0 && is_power_of_2(imm)); case BPF_DIV: case BPF_MOD: /* imm must be an 17-bit power of two */ return (u32)imm <= 0x10000 && is_power_of_2((u32)imm); } return false; } /* Rewrite ALU immediate operation */ bool rewrite_alu_i(u8 op, s32 imm, u8 *alu, s32 *val) { bool act = true; switch (BPF_OP(op)) { case BPF_LSH: case BPF_RSH: case BPF_ARSH: case BPF_ADD: case BPF_SUB: case BPF_OR: case BPF_XOR: /* imm == 0 is a no-op */ act = imm != 0; break; case BPF_MUL: if (imm == 1) { /* dst * 1 is a no-op */ act = false; } else if (imm == 0) { /* dst * 0 is dst & 0 */ op = BPF_AND; } else { /* dst * (1 << n) is dst << n */ op = BPF_LSH; imm = ilog2(abs(imm)); } break; case BPF_DIV: if (imm == 1) { /* dst / 1 is a no-op */ act = false; } else { /* dst / (1 << n) is dst >> n */ op = BPF_RSH; imm = ilog2(imm); } break; case BPF_MOD: /* dst % (1 << n) is dst & ((1 << n) - 1) */ op = BPF_AND; imm--; break; } *alu = op; *val = imm; return act; } /* ALU immediate operation (32-bit) */ void emit_alu_i(struct jit_context *ctx, u8 dst, s32 imm, u8 op) { switch (BPF_OP(op)) { /* dst = -dst */ case BPF_NEG: emit(ctx, subu, dst, MIPS_R_ZERO, dst); break; /* dst = dst & imm */ case BPF_AND: emit(ctx, andi, dst, dst, (u16)imm); break; /* dst = dst | imm */ case BPF_OR: emit(ctx, ori, dst, dst, (u16)imm); break; /* dst = dst ^ imm */ case BPF_XOR: emit(ctx, xori, dst, dst, (u16)imm); break; /* dst = dst << imm */ case BPF_LSH: emit(ctx, sll, dst, dst, imm); break; /* dst = dst >> imm */ case BPF_RSH: emit(ctx, srl, dst, dst, imm); break; /* dst = dst >> imm (arithmetic) */ case BPF_ARSH: emit(ctx, sra, dst, dst, imm); break; /* dst = dst + imm */ case BPF_ADD: emit(ctx, addiu, dst, dst, imm); break; /* dst = dst - imm */ case BPF_SUB: emit(ctx, addiu, dst, dst, -imm); break; } clobber_reg(ctx, dst); } /* ALU register operation (32-bit) */ void emit_alu_r(struct jit_context *ctx, u8 dst, u8 src, u8 op) { switch (BPF_OP(op)) { /* dst = dst & src */ case BPF_AND: emit(ctx, and, dst, dst, src); break; /* dst = dst | src */ case BPF_OR: emit(ctx, or, dst, dst, src); break; /* dst = dst ^ src */ case BPF_XOR: emit(ctx, xor, dst, dst, src); break; /* dst = dst << src */ case BPF_LSH: emit(ctx, sllv, dst, dst, src); break; /* dst = dst >> src */ case BPF_RSH: emit(ctx, srlv, dst, dst, src); break; /* dst = dst >> src (arithmetic) */ case BPF_ARSH: emit(ctx, srav, dst, dst, src); break; /* dst = dst + src */ case BPF_ADD: emit(ctx, addu, dst, dst, src); break; /* dst = dst - src */ case BPF_SUB: emit(ctx, subu, dst, dst, src); break; /* dst = dst * src */ case BPF_MUL: if (cpu_has_mips32r1 || cpu_has_mips32r6) { emit(ctx, mul, dst, dst, src); } else { emit(ctx, multu, dst, src); emit(ctx, mflo, dst); } break; /* dst = dst / src */ case BPF_DIV: if (cpu_has_mips32r6) { emit(ctx, divu_r6, dst, dst, src); } else { emit(ctx, divu, dst, src); emit(ctx, mflo, dst); } break; /* dst = dst % src */ case BPF_MOD: if (cpu_has_mips32r6) { emit(ctx, modu, dst, dst, src); } else { emit(ctx, divu, dst, src); emit(ctx, mfhi, dst); } break; } clobber_reg(ctx, dst); } /* Atomic read-modify-write (32-bit) */ void emit_atomic_r(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code) { LLSC_sync(ctx); emit(ctx, ll, MIPS_R_T9, off, dst); switch (code) { case BPF_ADD: case BPF_ADD | BPF_FETCH: emit(ctx, addu, MIPS_R_T8, MIPS_R_T9, src); break; case BPF_AND: case BPF_AND | BPF_FETCH: emit(ctx, and, MIPS_R_T8, MIPS_R_T9, src); break; case BPF_OR: case BPF_OR | BPF_FETCH: emit(ctx, or, MIPS_R_T8, MIPS_R_T9, src); break; case BPF_XOR: case BPF_XOR | BPF_FETCH: emit(ctx, xor, MIPS_R_T8, MIPS_R_T9, src); break; case BPF_XCHG: emit(ctx, move, MIPS_R_T8, src); break; } emit(ctx, sc, MIPS_R_T8, off, dst); emit(ctx, LLSC_beqz, MIPS_R_T8, -16 - LLSC_offset); emit(ctx, nop); /* Delay slot */ if (code & BPF_FETCH) { emit(ctx, move, src, MIPS_R_T9); clobber_reg(ctx, src); } } /* Atomic compare-and-exchange (32-bit) */ void emit_cmpxchg_r(struct jit_context *ctx, u8 dst, u8 src, u8 res, s16 off) { LLSC_sync(ctx); emit(ctx, ll, MIPS_R_T9, off, dst); emit(ctx, bne, MIPS_R_T9, res, 12); emit(ctx, move, MIPS_R_T8, src); /* Delay slot */ emit(ctx, sc, MIPS_R_T8, off, dst); emit(ctx, LLSC_beqz, MIPS_R_T8, -20 - LLSC_offset); emit(ctx, move, res, MIPS_R_T9); /* Delay slot */ clobber_reg(ctx, res); } /* Swap bytes and truncate a register word or half word */ void emit_bswap_r(struct jit_context *ctx, u8 dst, u32 width) { u8 tmp = MIPS_R_T8; u8 msk = MIPS_R_T9; switch (width) { /* Swap bytes in a word */ case 32: if (cpu_has_mips32r2 || cpu_has_mips32r6) { emit(ctx, wsbh, dst, dst); emit(ctx, rotr, dst, dst, 16); } else { emit(ctx, sll, tmp, dst, 16); /* tmp = dst << 16 */ emit(ctx, srl, dst, dst, 16); /* dst = dst >> 16 */ emit(ctx, or, dst, dst, tmp); /* dst = dst | tmp */ emit(ctx, lui, msk, 0xff); /* msk = 0x00ff0000 */ emit(ctx, ori, msk, msk, 0xff); /* msk = msk | 0xff */ emit(ctx, and, tmp, dst, msk); /* tmp = dst & msk */ emit(ctx, sll, tmp, tmp, 8); /* tmp = tmp << 8 */ emit(ctx, srl, dst, dst, 8); /* dst = dst >> 8 */ emit(ctx, and, dst, dst, msk); /* dst = dst & msk */ emit(ctx, or, dst, dst, tmp); /* reg = dst | tmp */ } break; /* Swap bytes in a half word */ case 16: if (cpu_has_mips32r2 || cpu_has_mips32r6) { emit(ctx, wsbh, dst, dst); emit(ctx, andi, dst, dst, 0xffff); } else { emit(ctx, andi, tmp, dst, 0xff00); /* t = d & 0xff00 */ emit(ctx, srl, tmp, tmp, 8); /* t = t >> 8 */ emit(ctx, andi, dst, dst, 0x00ff); /* d = d & 0x00ff */ emit(ctx, sll, dst, dst, 8); /* d = d << 8 */ emit(ctx, or, dst, dst, tmp); /* d = d | t */ } break; } clobber_reg(ctx, dst); } /* Validate jump immediate range */ bool valid_jmp_i(u8 op, s32 imm) { switch (op) { case JIT_JNOP: /* Immediate value not used */ return true; case BPF_JEQ: case BPF_JNE: /* No immediate operation */ return false; case BPF_JSET: case JIT_JNSET: /* imm must be 16 bits unsigned */ return imm >= 0 && imm <= 0xffff; case BPF_JGE: case BPF_JLT: case BPF_JSGE: case BPF_JSLT: /* imm must be 16 bits */ return imm >= -0x8000 && imm <= 0x7fff; case BPF_JGT: case BPF_JLE: case BPF_JSGT: case BPF_JSLE: /* imm + 1 must be 16 bits */ return imm >= -0x8001 && imm <= 0x7ffe; } return false; } /* Invert a conditional jump operation */ static u8 invert_jmp(u8 op) { switch (op) { case BPF_JA: return JIT_JNOP; case BPF_JEQ: return BPF_JNE; case BPF_JNE: return BPF_JEQ; case BPF_JSET: return JIT_JNSET; case BPF_JGT: return BPF_JLE; case BPF_JGE: return BPF_JLT; case BPF_JLT: return BPF_JGE; case BPF_JLE: return BPF_JGT; case BPF_JSGT: return BPF_JSLE; case BPF_JSGE: return BPF_JSLT; case BPF_JSLT: return BPF_JSGE; case BPF_JSLE: return BPF_JSGT; } return 0; } /* Prepare a PC-relative jump operation */ static void setup_jmp(struct jit_context *ctx, u8 bpf_op, s16 bpf_off, u8 *jit_op, s32 *jit_off) { u32 *descp = &ctx->descriptors[ctx->bpf_index]; int op = bpf_op; int offset = 0; /* Do not compute offsets on the first pass */ if (INDEX(*descp) == 0) goto done; /* Skip jumps never taken */ if (bpf_op == JIT_JNOP) goto done; /* Convert jumps always taken */ if (bpf_op == BPF_JA) *descp |= JIT_DESC_CONVERT; /* * Current ctx->jit_index points to the start of the branch preamble. * Since the preamble differs among different branch conditionals, * the current index cannot be used to compute the branch offset. * Instead, we use the offset table value for the next instruction, * which gives the index immediately after the branch delay slot. */ if (!CONVERTED(*descp)) { int target = ctx->bpf_index + bpf_off + 1; int origin = ctx->bpf_index + 1; offset = (INDEX(ctx->descriptors[target]) - INDEX(ctx->descriptors[origin]) + 1) * sizeof(u32); } /* * The PC-relative branch offset field on MIPS is 18 bits signed, * so if the computed offset is larger than this we generate a an * absolute jump that we skip with an inverted conditional branch. */ if (CONVERTED(*descp) || offset < -0x20000 || offset > 0x1ffff) { offset = 3 * sizeof(u32); op = invert_jmp(bpf_op); ctx->changes += !CONVERTED(*descp); *descp |= JIT_DESC_CONVERT; } done: *jit_off = offset; *jit_op = op; } /* Prepare a PC-relative jump operation with immediate conditional */ void setup_jmp_i(struct jit_context *ctx, s32 imm, u8 width, u8 bpf_op, s16 bpf_off, u8 *jit_op, s32 *jit_off) { bool always = false; bool never = false; switch (bpf_op) { case BPF_JEQ: case BPF_JNE: break; case BPF_JSET: case BPF_JLT: never = imm == 0; break; case BPF_JGE: always = imm == 0; break; case BPF_JGT: never = (u32)imm == U32_MAX; break; case BPF_JLE: always = (u32)imm == U32_MAX; break; case BPF_JSGT: never = imm == S32_MAX && width == 32; break; case BPF_JSGE: always = imm == S32_MIN && width == 32; break; case BPF_JSLT: never = imm == S32_MIN && width == 32; break; case BPF_JSLE: always = imm == S32_MAX && width == 32; break; } if (never) bpf_op = JIT_JNOP; if (always) bpf_op = BPF_JA; setup_jmp(ctx, bpf_op, bpf_off, jit_op, jit_off); } /* Prepare a PC-relative jump operation with register conditional */ void setup_jmp_r(struct jit_context *ctx, bool same_reg, u8 bpf_op, s16 bpf_off, u8 *jit_op, s32 *jit_off) { switch (bpf_op) { case BPF_JSET: break; case BPF_JEQ: case BPF_JGE: case BPF_JLE: case BPF_JSGE: case BPF_JSLE: if (same_reg) bpf_op = BPF_JA; break; case BPF_JNE: case BPF_JLT: case BPF_JGT: case BPF_JSGT: case BPF_JSLT: if (same_reg) bpf_op = JIT_JNOP; break; } setup_jmp(ctx, bpf_op, bpf_off, jit_op, jit_off); } /* Finish a PC-relative jump operation */ int finish_jmp(struct jit_context *ctx, u8 jit_op, s16 bpf_off) { /* Emit conditional branch delay slot */ if (jit_op != JIT_JNOP) emit(ctx, nop); /* * Emit an absolute long jump with delay slot, * if the PC-relative branch was converted. */ if (CONVERTED(ctx->descriptors[ctx->bpf_index])) { int target = get_target(ctx, ctx->bpf_index + bpf_off + 1); if (target < 0) return -1; emit(ctx, j, target); emit(ctx, nop); } return 0; } /* Jump immediate (32-bit) */ void emit_jmp_i(struct jit_context *ctx, u8 dst, s32 imm, s32 off, u8 op) { switch (op) { /* No-op, used internally for branch optimization */ case JIT_JNOP: break; /* PC += off if dst & imm */ case BPF_JSET: emit(ctx, andi, MIPS_R_T9, dst, (u16)imm); emit(ctx, bnez, MIPS_R_T9, off); break; /* PC += off if (dst & imm) == 0 (not in BPF, used for long jumps) */ case JIT_JNSET: emit(ctx, andi, MIPS_R_T9, dst, (u16)imm); emit(ctx, beqz, MIPS_R_T9, off); break; /* PC += off if dst > imm */ case BPF_JGT: emit(ctx, sltiu, MIPS_R_T9, dst, imm + 1); emit(ctx, beqz, MIPS_R_T9, off); break; /* PC += off if dst >= imm */ case BPF_JGE: emit(ctx, sltiu, MIPS_R_T9, dst, imm); emit(ctx, beqz, MIPS_R_T9, off); break; /* PC += off if dst < imm */ case BPF_JLT: emit(ctx, sltiu, MIPS_R_T9, dst, imm); emit(ctx, bnez, MIPS_R_T9, off); break; /* PC += off if dst <= imm */ case BPF_JLE: emit(ctx, sltiu, MIPS_R_T9, dst, imm + 1); emit(ctx, bnez, MIPS_R_T9, off); break; /* PC += off if dst > imm (signed) */ case BPF_JSGT: emit(ctx, slti, MIPS_R_T9, dst, imm + 1); emit(ctx, beqz, MIPS_R_T9, off); break; /* PC += off if dst >= imm (signed) */ case BPF_JSGE: emit(ctx, slti, MIPS_R_T9, dst, imm); emit(ctx, beqz, MIPS_R_T9, off); break; /* PC += off if dst < imm (signed) */ case BPF_JSLT: emit(ctx, slti, MIPS_R_T9, dst, imm); emit(ctx, bnez, MIPS_R_T9, off); break; /* PC += off if dst <= imm (signed) */ case BPF_JSLE: emit(ctx, slti, MIPS_R_T9, dst, imm + 1); emit(ctx, bnez, MIPS_R_T9, off); break; } } /* Jump register (32-bit) */ void emit_jmp_r(struct jit_context *ctx, u8 dst, u8 src, s32 off, u8 op) { switch (op) { /* No-op, used internally for branch optimization */ case JIT_JNOP: break; /* PC += off if dst == src */ case BPF_JEQ: emit(ctx, beq, dst, src, off); break; /* PC += off if dst != src */ case BPF_JNE: emit(ctx, bne, dst, src, off); break; /* PC += off if dst & src */ case BPF_JSET: emit(ctx, and, MIPS_R_T9, dst, src); emit(ctx, bnez, MIPS_R_T9, off); break; /* PC += off if (dst & imm) == 0 (not in BPF, used for long jumps) */ case JIT_JNSET: emit(ctx, and, MIPS_R_T9, dst, src); emit(ctx, beqz, MIPS_R_T9, off); break; /* PC += off if dst > src */ case BPF_JGT: emit(ctx, sltu, MIPS_R_T9, src, dst); emit(ctx, bnez, MIPS_R_T9, off); break; /* PC += off if dst >= src */ case BPF_JGE: emit(ctx, sltu, MIPS_R_T9, dst, src); emit(ctx, beqz, MIPS_R_T9, off); break; /* PC += off if dst < src */ case BPF_JLT: emit(ctx, sltu, MIPS_R_T9, dst, src); emit(ctx, bnez, MIPS_R_T9, off); break; /* PC += off if dst <= src */ case BPF_JLE: emit(ctx, sltu, MIPS_R_T9, src, dst); emit(ctx, beqz, MIPS_R_T9, off); break; /* PC += off if dst > src (signed) */ case BPF_JSGT: emit(ctx, slt, MIPS_R_T9, src, dst); emit(ctx, bnez, MIPS_R_T9, off); break; /* PC += off if dst >= src (signed) */ case BPF_JSGE: emit(ctx, slt, MIPS_R_T9, dst, src); emit(ctx, beqz, MIPS_R_T9, off); break; /* PC += off if dst < src (signed) */ case BPF_JSLT: emit(ctx, slt, MIPS_R_T9, dst, src); emit(ctx, bnez, MIPS_R_T9, off); break; /* PC += off if dst <= src (signed) */ case BPF_JSLE: emit(ctx, slt, MIPS_R_T9, src, dst); emit(ctx, beqz, MIPS_R_T9, off); break; } } /* Jump always */ int emit_ja(struct jit_context *ctx, s16 off) { int target = get_target(ctx, ctx->bpf_index + off + 1); if (target < 0) return -1; emit(ctx, j, target); emit(ctx, nop); return 0; } /* Jump to epilogue */ int emit_exit(struct jit_context *ctx) { int target = get_target(ctx, ctx->program->len); if (target < 0) return -1; emit(ctx, j, target); emit(ctx, nop); return 0; } /* Build the program body from eBPF bytecode */ static int build_body(struct jit_context *ctx) { const struct bpf_prog *prog = ctx->program; unsigned int i; ctx->stack_used = 0; for (i = 0; i < prog->len; i++) { const struct bpf_insn *insn = &prog->insnsi[i]; u32 *descp = &ctx->descriptors[i]; int ret; access_reg(ctx, insn->src_reg); access_reg(ctx, insn->dst_reg); ctx->bpf_index = i; if (ctx->target == NULL) { ctx->changes += INDEX(*descp) != ctx->jit_index; *descp &= JIT_DESC_CONVERT; *descp |= ctx->jit_index; } ret = build_insn(insn, ctx); if (ret < 0) return ret; if (ret > 0) { i++; if (ctx->target == NULL) descp[1] = ctx->jit_index; } } /* Store the end offset, where the epilogue begins */ ctx->descriptors[prog->len] = ctx->jit_index; return 0; } /* Set the branch conversion flag on all instructions */ static void set_convert_flag(struct jit_context *ctx, bool enable) { const struct bpf_prog *prog = ctx->program; u32 flag = enable ? JIT_DESC_CONVERT : 0; unsigned int i; for (i = 0; i <= prog->len; i++) ctx->descriptors[i] = INDEX(ctx->descriptors[i]) | flag; } static void jit_fill_hole(void *area, unsigned int size) { u32 *p; /* We are guaranteed to have aligned memory. */ for (p = area; size >= sizeof(u32); size -= sizeof(u32)) uasm_i_break(&p, BRK_BUG); /* Increments p */ } bool bpf_jit_needs_zext(void) { return true; } struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) { struct bpf_prog *tmp, *orig_prog = prog; struct bpf_binary_header *header = NULL; struct jit_context ctx; bool tmp_blinded = false; unsigned int tmp_idx; unsigned int image_size; u8 *image_ptr; int tries; /* * If BPF JIT was not enabled then we must fall back to * the interpreter. */ if (!prog->jit_requested) return orig_prog; /* * If constant blinding was enabled and we failed during blinding * then we must fall back to the interpreter. Otherwise, we save * the new JITed code. */ tmp = bpf_jit_blind_constants(prog); if (IS_ERR(tmp)) return orig_prog; if (tmp != prog) { tmp_blinded = true; prog = tmp; } memset(&ctx, 0, sizeof(ctx)); ctx.program = prog; /* * Not able to allocate memory for descriptors[], then * we must fall back to the interpreter */ ctx.descriptors = kcalloc(prog->len + 1, sizeof(*ctx.descriptors), GFP_KERNEL); if (ctx.descriptors == NULL) goto out_err; /* First pass discovers used resources */ if (build_body(&ctx) < 0) goto out_err; /* * Second pass computes instruction offsets. * If any PC-relative branches are out of range, a sequence of * a PC-relative branch + a jump is generated, and we have to * try again from the beginning to generate the new offsets. * This is done until no additional conversions are necessary. * The last two iterations are done with all branches being * converted, to guarantee offset table convergence within a * fixed number of iterations. */ ctx.jit_index = 0; build_prologue(&ctx); tmp_idx = ctx.jit_index; tries = JIT_MAX_ITERATIONS; do { ctx.jit_index = tmp_idx; ctx.changes = 0; if (tries == 2) set_convert_flag(&ctx, true); if (build_body(&ctx) < 0) goto out_err; } while (ctx.changes > 0 && --tries > 0); if (WARN_ONCE(ctx.changes > 0, "JIT offsets failed to converge")) goto out_err; build_epilogue(&ctx, MIPS_R_RA); /* Now we know the size of the structure to make */ image_size = sizeof(u32) * ctx.jit_index; header = bpf_jit_binary_alloc(image_size, &image_ptr, sizeof(u32), jit_fill_hole); /* * Not able to allocate memory for the structure then * we must fall back to the interpretation */ if (header == NULL) goto out_err; /* Actual pass to generate final JIT code */ ctx.target = (u32 *)image_ptr; ctx.jit_index = 0; /* * If building the JITed code fails somehow, * we fall back to the interpretation. */ build_prologue(&ctx); if (build_body(&ctx) < 0) goto out_err; build_epilogue(&ctx, MIPS_R_RA); /* Populate line info meta data */ set_convert_flag(&ctx, false); bpf_prog_fill_jited_linfo(prog, &ctx.descriptors[1]); /* Set as read-only exec and flush instruction cache */ bpf_jit_binary_lock_ro(header); flush_icache_range((unsigned long)header, (unsigned long)&ctx.target[ctx.jit_index]); if (bpf_jit_enable > 1) bpf_jit_dump(prog->len, image_size, 2, ctx.target); prog->bpf_func = (void *)ctx.target; prog->jited = 1; prog->jited_len = image_size; out: if (tmp_blinded) bpf_jit_prog_release_other(prog, prog == orig_prog ? tmp : orig_prog); kfree(ctx.descriptors); return prog; out_err: prog = orig_prog; if (header) bpf_jit_binary_free(header); goto out; }
linux-master
arch/mips/net/bpf_jit_comp.c
// SPDX-License-Identifier: GPL-2.0-only /* * Just-In-Time compiler for eBPF bytecode on MIPS. * Implementation of JIT functions for 32-bit CPUs. * * Copyright (c) 2021 Anyfi Networks AB. * Author: Johan Almbladh <[email protected]> * * Based on code and ideas from * Copyright (c) 2017 Cavium, Inc. * Copyright (c) 2017 Shubham Bansal <[email protected]> * Copyright (c) 2011 Mircea Gherzan <[email protected]> */ #include <linux/math64.h> #include <linux/errno.h> #include <linux/filter.h> #include <linux/bpf.h> #include <asm/cpu-features.h> #include <asm/isa-rev.h> #include <asm/uasm.h> #include "bpf_jit_comp.h" /* MIPS a4-a7 are not available in the o32 ABI */ #undef MIPS_R_A4 #undef MIPS_R_A5 #undef MIPS_R_A6 #undef MIPS_R_A7 /* Stack is 8-byte aligned in o32 ABI */ #define MIPS_STACK_ALIGNMENT 8 /* * The top 16 bytes of a stack frame is reserved for the callee in O32 ABI. * This corresponds to stack space for register arguments a0-a3. */ #define JIT_RESERVED_STACK 16 /* Temporary 64-bit register used by JIT */ #define JIT_REG_TMP MAX_BPF_JIT_REG /* * Number of prologue bytes to skip when doing a tail call. * Tail call count (TCC) initialization (8 bytes) always, plus * R0-to-v0 assignment (4 bytes) if big endian. */ #ifdef __BIG_ENDIAN #define JIT_TCALL_SKIP 12 #else #define JIT_TCALL_SKIP 8 #endif /* CPU registers holding the callee return value */ #define JIT_RETURN_REGS \ (BIT(MIPS_R_V0) | \ BIT(MIPS_R_V1)) /* CPU registers arguments passed to callee directly */ #define JIT_ARG_REGS \ (BIT(MIPS_R_A0) | \ BIT(MIPS_R_A1) | \ BIT(MIPS_R_A2) | \ BIT(MIPS_R_A3)) /* CPU register arguments passed to callee on stack */ #define JIT_STACK_REGS \ (BIT(MIPS_R_T0) | \ BIT(MIPS_R_T1) | \ BIT(MIPS_R_T2) | \ BIT(MIPS_R_T3) | \ BIT(MIPS_R_T4) | \ BIT(MIPS_R_T5)) /* Caller-saved CPU registers */ #define JIT_CALLER_REGS \ (JIT_RETURN_REGS | \ JIT_ARG_REGS | \ JIT_STACK_REGS) /* Callee-saved CPU registers */ #define JIT_CALLEE_REGS \ (BIT(MIPS_R_S0) | \ BIT(MIPS_R_S1) | \ BIT(MIPS_R_S2) | \ BIT(MIPS_R_S3) | \ BIT(MIPS_R_S4) | \ BIT(MIPS_R_S5) | \ BIT(MIPS_R_S6) | \ BIT(MIPS_R_S7) | \ BIT(MIPS_R_GP) | \ BIT(MIPS_R_FP) | \ BIT(MIPS_R_RA)) /* * Mapping of 64-bit eBPF registers to 32-bit native MIPS registers. * * 1) Native register pairs are ordered according to CPU endiannes, following * the MIPS convention for passing 64-bit arguments and return values. * 2) The eBPF return value, arguments and callee-saved registers are mapped * to their native MIPS equivalents. * 3) Since the 32 highest bits in the eBPF FP register are always zero, * only one general-purpose register is actually needed for the mapping. * We use the fp register for this purpose, and map the highest bits to * the MIPS register r0 (zero). * 4) We use the MIPS gp and at registers as internal temporary registers * for constant blinding. The gp register is callee-saved. * 5) One 64-bit temporary register is mapped for use when sign-extending * immediate operands. MIPS registers t6-t9 are available to the JIT * for as temporaries when implementing complex 64-bit operations. * * With this scheme all eBPF registers are being mapped to native MIPS * registers without having to use any stack scratch space. The direct * register mapping (2) simplifies the handling of function calls. */ static const u8 bpf2mips32[][2] = { /* Return value from in-kernel function, and exit value from eBPF */ [BPF_REG_0] = {MIPS_R_V1, MIPS_R_V0}, /* Arguments from eBPF program to in-kernel function */ [BPF_REG_1] = {MIPS_R_A1, MIPS_R_A0}, [BPF_REG_2] = {MIPS_R_A3, MIPS_R_A2}, /* Remaining arguments, to be passed on the stack per O32 ABI */ [BPF_REG_3] = {MIPS_R_T1, MIPS_R_T0}, [BPF_REG_4] = {MIPS_R_T3, MIPS_R_T2}, [BPF_REG_5] = {MIPS_R_T5, MIPS_R_T4}, /* Callee-saved registers that in-kernel function will preserve */ [BPF_REG_6] = {MIPS_R_S1, MIPS_R_S0}, [BPF_REG_7] = {MIPS_R_S3, MIPS_R_S2}, [BPF_REG_8] = {MIPS_R_S5, MIPS_R_S4}, [BPF_REG_9] = {MIPS_R_S7, MIPS_R_S6}, /* Read-only frame pointer to access the eBPF stack */ #ifdef __BIG_ENDIAN [BPF_REG_FP] = {MIPS_R_FP, MIPS_R_ZERO}, #else [BPF_REG_FP] = {MIPS_R_ZERO, MIPS_R_FP}, #endif /* Temporary register for blinding constants */ [BPF_REG_AX] = {MIPS_R_GP, MIPS_R_AT}, /* Temporary register for internal JIT use */ [JIT_REG_TMP] = {MIPS_R_T7, MIPS_R_T6}, }; /* Get low CPU register for a 64-bit eBPF register mapping */ static inline u8 lo(const u8 reg[]) { #ifdef __BIG_ENDIAN return reg[0]; #else return reg[1]; #endif } /* Get high CPU register for a 64-bit eBPF register mapping */ static inline u8 hi(const u8 reg[]) { #ifdef __BIG_ENDIAN return reg[1]; #else return reg[0]; #endif } /* * Mark a 64-bit CPU register pair as clobbered, it needs to be * saved/restored by the program if callee-saved. */ static void clobber_reg64(struct jit_context *ctx, const u8 reg[]) { clobber_reg(ctx, reg[0]); clobber_reg(ctx, reg[1]); } /* dst = imm (sign-extended) */ static void emit_mov_se_i64(struct jit_context *ctx, const u8 dst[], s32 imm) { emit_mov_i(ctx, lo(dst), imm); if (imm < 0) emit(ctx, addiu, hi(dst), MIPS_R_ZERO, -1); else emit(ctx, move, hi(dst), MIPS_R_ZERO); clobber_reg64(ctx, dst); } /* Zero extension, if verifier does not do it for us */ static void emit_zext_ver(struct jit_context *ctx, const u8 dst[]) { if (!ctx->program->aux->verifier_zext) { emit(ctx, move, hi(dst), MIPS_R_ZERO); clobber_reg(ctx, hi(dst)); } } /* Load delay slot, if ISA mandates it */ static void emit_load_delay(struct jit_context *ctx) { if (!cpu_has_mips_2_3_4_5_r) emit(ctx, nop); } /* ALU immediate operation (64-bit) */ static void emit_alu_i64(struct jit_context *ctx, const u8 dst[], s32 imm, u8 op) { u8 src = MIPS_R_T6; /* * ADD/SUB with all but the max negative imm can be handled by * inverting the operation and the imm value, saving one insn. */ if (imm > S32_MIN && imm < 0) switch (op) { case BPF_ADD: op = BPF_SUB; imm = -imm; break; case BPF_SUB: op = BPF_ADD; imm = -imm; break; } /* Move immediate to temporary register */ emit_mov_i(ctx, src, imm); switch (op) { /* dst = dst + imm */ case BPF_ADD: emit(ctx, addu, lo(dst), lo(dst), src); emit(ctx, sltu, MIPS_R_T9, lo(dst), src); emit(ctx, addu, hi(dst), hi(dst), MIPS_R_T9); if (imm < 0) emit(ctx, addiu, hi(dst), hi(dst), -1); break; /* dst = dst - imm */ case BPF_SUB: emit(ctx, sltu, MIPS_R_T9, lo(dst), src); emit(ctx, subu, lo(dst), lo(dst), src); emit(ctx, subu, hi(dst), hi(dst), MIPS_R_T9); if (imm < 0) emit(ctx, addiu, hi(dst), hi(dst), 1); break; /* dst = dst | imm */ case BPF_OR: emit(ctx, or, lo(dst), lo(dst), src); if (imm < 0) emit(ctx, addiu, hi(dst), MIPS_R_ZERO, -1); break; /* dst = dst & imm */ case BPF_AND: emit(ctx, and, lo(dst), lo(dst), src); if (imm >= 0) emit(ctx, move, hi(dst), MIPS_R_ZERO); break; /* dst = dst ^ imm */ case BPF_XOR: emit(ctx, xor, lo(dst), lo(dst), src); if (imm < 0) { emit(ctx, subu, hi(dst), MIPS_R_ZERO, hi(dst)); emit(ctx, addiu, hi(dst), hi(dst), -1); } break; } clobber_reg64(ctx, dst); } /* ALU register operation (64-bit) */ static void emit_alu_r64(struct jit_context *ctx, const u8 dst[], const u8 src[], u8 op) { switch (BPF_OP(op)) { /* dst = dst + src */ case BPF_ADD: if (src == dst) { emit(ctx, srl, MIPS_R_T9, lo(dst), 31); emit(ctx, addu, lo(dst), lo(dst), lo(dst)); } else { emit(ctx, addu, lo(dst), lo(dst), lo(src)); emit(ctx, sltu, MIPS_R_T9, lo(dst), lo(src)); } emit(ctx, addu, hi(dst), hi(dst), hi(src)); emit(ctx, addu, hi(dst), hi(dst), MIPS_R_T9); break; /* dst = dst - src */ case BPF_SUB: emit(ctx, sltu, MIPS_R_T9, lo(dst), lo(src)); emit(ctx, subu, lo(dst), lo(dst), lo(src)); emit(ctx, subu, hi(dst), hi(dst), hi(src)); emit(ctx, subu, hi(dst), hi(dst), MIPS_R_T9); break; /* dst = dst | src */ case BPF_OR: emit(ctx, or, lo(dst), lo(dst), lo(src)); emit(ctx, or, hi(dst), hi(dst), hi(src)); break; /* dst = dst & src */ case BPF_AND: emit(ctx, and, lo(dst), lo(dst), lo(src)); emit(ctx, and, hi(dst), hi(dst), hi(src)); break; /* dst = dst ^ src */ case BPF_XOR: emit(ctx, xor, lo(dst), lo(dst), lo(src)); emit(ctx, xor, hi(dst), hi(dst), hi(src)); break; } clobber_reg64(ctx, dst); } /* ALU invert (64-bit) */ static void emit_neg_i64(struct jit_context *ctx, const u8 dst[]) { emit(ctx, sltu, MIPS_R_T9, MIPS_R_ZERO, lo(dst)); emit(ctx, subu, lo(dst), MIPS_R_ZERO, lo(dst)); emit(ctx, subu, hi(dst), MIPS_R_ZERO, hi(dst)); emit(ctx, subu, hi(dst), hi(dst), MIPS_R_T9); clobber_reg64(ctx, dst); } /* ALU shift immediate (64-bit) */ static void emit_shift_i64(struct jit_context *ctx, const u8 dst[], u32 imm, u8 op) { switch (BPF_OP(op)) { /* dst = dst << imm */ case BPF_LSH: if (imm < 32) { emit(ctx, srl, MIPS_R_T9, lo(dst), 32 - imm); emit(ctx, sll, lo(dst), lo(dst), imm); emit(ctx, sll, hi(dst), hi(dst), imm); emit(ctx, or, hi(dst), hi(dst), MIPS_R_T9); } else { emit(ctx, sll, hi(dst), lo(dst), imm - 32); emit(ctx, move, lo(dst), MIPS_R_ZERO); } break; /* dst = dst >> imm */ case BPF_RSH: if (imm < 32) { emit(ctx, sll, MIPS_R_T9, hi(dst), 32 - imm); emit(ctx, srl, lo(dst), lo(dst), imm); emit(ctx, srl, hi(dst), hi(dst), imm); emit(ctx, or, lo(dst), lo(dst), MIPS_R_T9); } else { emit(ctx, srl, lo(dst), hi(dst), imm - 32); emit(ctx, move, hi(dst), MIPS_R_ZERO); } break; /* dst = dst >> imm (arithmetic) */ case BPF_ARSH: if (imm < 32) { emit(ctx, sll, MIPS_R_T9, hi(dst), 32 - imm); emit(ctx, srl, lo(dst), lo(dst), imm); emit(ctx, sra, hi(dst), hi(dst), imm); emit(ctx, or, lo(dst), lo(dst), MIPS_R_T9); } else { emit(ctx, sra, lo(dst), hi(dst), imm - 32); emit(ctx, sra, hi(dst), hi(dst), 31); } break; } clobber_reg64(ctx, dst); } /* ALU shift register (64-bit) */ static void emit_shift_r64(struct jit_context *ctx, const u8 dst[], u8 src, u8 op) { u8 t1 = MIPS_R_T8; u8 t2 = MIPS_R_T9; emit(ctx, andi, t1, src, 32); /* t1 = src & 32 */ emit(ctx, beqz, t1, 16); /* PC += 16 if t1 == 0 */ emit(ctx, nor, t2, src, MIPS_R_ZERO); /* t2 = ~src (delay slot) */ switch (BPF_OP(op)) { /* dst = dst << src */ case BPF_LSH: /* Next: shift >= 32 */ emit(ctx, sllv, hi(dst), lo(dst), src); /* dh = dl << src */ emit(ctx, move, lo(dst), MIPS_R_ZERO); /* dl = 0 */ emit(ctx, b, 20); /* PC += 20 */ /* +16: shift < 32 */ emit(ctx, srl, t1, lo(dst), 1); /* t1 = dl >> 1 */ emit(ctx, srlv, t1, t1, t2); /* t1 = t1 >> t2 */ emit(ctx, sllv, lo(dst), lo(dst), src); /* dl = dl << src */ emit(ctx, sllv, hi(dst), hi(dst), src); /* dh = dh << src */ emit(ctx, or, hi(dst), hi(dst), t1); /* dh = dh | t1 */ break; /* dst = dst >> src */ case BPF_RSH: /* Next: shift >= 32 */ emit(ctx, srlv, lo(dst), hi(dst), src); /* dl = dh >> src */ emit(ctx, move, hi(dst), MIPS_R_ZERO); /* dh = 0 */ emit(ctx, b, 20); /* PC += 20 */ /* +16: shift < 32 */ emit(ctx, sll, t1, hi(dst), 1); /* t1 = dl << 1 */ emit(ctx, sllv, t1, t1, t2); /* t1 = t1 << t2 */ emit(ctx, srlv, lo(dst), lo(dst), src); /* dl = dl >> src */ emit(ctx, srlv, hi(dst), hi(dst), src); /* dh = dh >> src */ emit(ctx, or, lo(dst), lo(dst), t1); /* dl = dl | t1 */ break; /* dst = dst >> src (arithmetic) */ case BPF_ARSH: /* Next: shift >= 32 */ emit(ctx, srav, lo(dst), hi(dst), src); /* dl = dh >>a src */ emit(ctx, sra, hi(dst), hi(dst), 31); /* dh = dh >>a 31 */ emit(ctx, b, 20); /* PC += 20 */ /* +16: shift < 32 */ emit(ctx, sll, t1, hi(dst), 1); /* t1 = dl << 1 */ emit(ctx, sllv, t1, t1, t2); /* t1 = t1 << t2 */ emit(ctx, srlv, lo(dst), lo(dst), src); /* dl = dl >>a src */ emit(ctx, srav, hi(dst), hi(dst), src); /* dh = dh >> src */ emit(ctx, or, lo(dst), lo(dst), t1); /* dl = dl | t1 */ break; } /* +20: Done */ clobber_reg64(ctx, dst); } /* ALU mul immediate (64x32-bit) */ static void emit_mul_i64(struct jit_context *ctx, const u8 dst[], s32 imm) { u8 src = MIPS_R_T6; u8 tmp = MIPS_R_T9; switch (imm) { /* dst = dst * 1 is a no-op */ case 1: break; /* dst = dst * -1 */ case -1: emit_neg_i64(ctx, dst); break; case 0: emit_mov_r(ctx, lo(dst), MIPS_R_ZERO); emit_mov_r(ctx, hi(dst), MIPS_R_ZERO); break; /* Full 64x32 multiply */ default: /* hi(dst) = hi(dst) * src(imm) */ emit_mov_i(ctx, src, imm); if (cpu_has_mips32r1 || cpu_has_mips32r6) { emit(ctx, mul, hi(dst), hi(dst), src); } else { emit(ctx, multu, hi(dst), src); emit(ctx, mflo, hi(dst)); } /* hi(dst) = hi(dst) - lo(dst) */ if (imm < 0) emit(ctx, subu, hi(dst), hi(dst), lo(dst)); /* tmp = lo(dst) * src(imm) >> 32 */ /* lo(dst) = lo(dst) * src(imm) */ if (cpu_has_mips32r6) { emit(ctx, muhu, tmp, lo(dst), src); emit(ctx, mulu, lo(dst), lo(dst), src); } else { emit(ctx, multu, lo(dst), src); emit(ctx, mflo, lo(dst)); emit(ctx, mfhi, tmp); } /* hi(dst) += tmp */ emit(ctx, addu, hi(dst), hi(dst), tmp); clobber_reg64(ctx, dst); break; } } /* ALU mul register (64x64-bit) */ static void emit_mul_r64(struct jit_context *ctx, const u8 dst[], const u8 src[]) { u8 acc = MIPS_R_T8; u8 tmp = MIPS_R_T9; /* acc = hi(dst) * lo(src) */ if (cpu_has_mips32r1 || cpu_has_mips32r6) { emit(ctx, mul, acc, hi(dst), lo(src)); } else { emit(ctx, multu, hi(dst), lo(src)); emit(ctx, mflo, acc); } /* tmp = lo(dst) * hi(src) */ if (cpu_has_mips32r1 || cpu_has_mips32r6) { emit(ctx, mul, tmp, lo(dst), hi(src)); } else { emit(ctx, multu, lo(dst), hi(src)); emit(ctx, mflo, tmp); } /* acc += tmp */ emit(ctx, addu, acc, acc, tmp); /* tmp = lo(dst) * lo(src) >> 32 */ /* lo(dst) = lo(dst) * lo(src) */ if (cpu_has_mips32r6) { emit(ctx, muhu, tmp, lo(dst), lo(src)); emit(ctx, mulu, lo(dst), lo(dst), lo(src)); } else { emit(ctx, multu, lo(dst), lo(src)); emit(ctx, mflo, lo(dst)); emit(ctx, mfhi, tmp); } /* hi(dst) = acc + tmp */ emit(ctx, addu, hi(dst), acc, tmp); clobber_reg64(ctx, dst); } /* Helper function for 64-bit modulo */ static u64 jit_mod64(u64 a, u64 b) { u64 rem; div64_u64_rem(a, b, &rem); return rem; } /* ALU div/mod register (64-bit) */ static void emit_divmod_r64(struct jit_context *ctx, const u8 dst[], const u8 src[], u8 op) { const u8 *r0 = bpf2mips32[BPF_REG_0]; /* Mapped to v0-v1 */ const u8 *r1 = bpf2mips32[BPF_REG_1]; /* Mapped to a0-a1 */ const u8 *r2 = bpf2mips32[BPF_REG_2]; /* Mapped to a2-a3 */ int exclude, k; u32 addr = 0; /* Push caller-saved registers on stack */ push_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, 0, JIT_RESERVED_STACK); /* Put 64-bit arguments 1 and 2 in registers a0-a3 */ for (k = 0; k < 2; k++) { emit(ctx, move, MIPS_R_T9, src[k]); emit(ctx, move, r1[k], dst[k]); emit(ctx, move, r2[k], MIPS_R_T9); } /* Emit function call */ switch (BPF_OP(op)) { /* dst = dst / src */ case BPF_DIV: addr = (u32)&div64_u64; break; /* dst = dst % src */ case BPF_MOD: addr = (u32)&jit_mod64; break; } emit_mov_i(ctx, MIPS_R_T9, addr); emit(ctx, jalr, MIPS_R_RA, MIPS_R_T9); emit(ctx, nop); /* Delay slot */ /* Store the 64-bit result in dst */ emit(ctx, move, dst[0], r0[0]); emit(ctx, move, dst[1], r0[1]); /* Restore caller-saved registers, excluding the computed result */ exclude = BIT(lo(dst)) | BIT(hi(dst)); pop_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, exclude, JIT_RESERVED_STACK); emit_load_delay(ctx); clobber_reg64(ctx, dst); clobber_reg(ctx, MIPS_R_V0); clobber_reg(ctx, MIPS_R_V1); clobber_reg(ctx, MIPS_R_RA); } /* Swap bytes in a register word */ static void emit_swap8_r(struct jit_context *ctx, u8 dst, u8 src, u8 mask) { u8 tmp = MIPS_R_T9; emit(ctx, and, tmp, src, mask); /* tmp = src & 0x00ff00ff */ emit(ctx, sll, tmp, tmp, 8); /* tmp = tmp << 8 */ emit(ctx, srl, dst, src, 8); /* dst = src >> 8 */ emit(ctx, and, dst, dst, mask); /* dst = dst & 0x00ff00ff */ emit(ctx, or, dst, dst, tmp); /* dst = dst | tmp */ } /* Swap half words in a register word */ static void emit_swap16_r(struct jit_context *ctx, u8 dst, u8 src) { u8 tmp = MIPS_R_T9; emit(ctx, sll, tmp, src, 16); /* tmp = src << 16 */ emit(ctx, srl, dst, src, 16); /* dst = src >> 16 */ emit(ctx, or, dst, dst, tmp); /* dst = dst | tmp */ } /* Swap bytes and truncate a register double word, word or half word */ static void emit_bswap_r64(struct jit_context *ctx, const u8 dst[], u32 width) { u8 tmp = MIPS_R_T8; switch (width) { /* Swap bytes in a double word */ case 64: if (cpu_has_mips32r2 || cpu_has_mips32r6) { emit(ctx, rotr, tmp, hi(dst), 16); emit(ctx, rotr, hi(dst), lo(dst), 16); emit(ctx, wsbh, lo(dst), tmp); emit(ctx, wsbh, hi(dst), hi(dst)); } else { emit_swap16_r(ctx, tmp, lo(dst)); emit_swap16_r(ctx, lo(dst), hi(dst)); emit(ctx, move, hi(dst), tmp); emit(ctx, lui, tmp, 0xff); /* tmp = 0x00ff0000 */ emit(ctx, ori, tmp, tmp, 0xff); /* tmp = 0x00ff00ff */ emit_swap8_r(ctx, lo(dst), lo(dst), tmp); emit_swap8_r(ctx, hi(dst), hi(dst), tmp); } break; /* Swap bytes in a word */ /* Swap bytes in a half word */ case 32: case 16: emit_bswap_r(ctx, lo(dst), width); emit(ctx, move, hi(dst), MIPS_R_ZERO); break; } clobber_reg64(ctx, dst); } /* Truncate a register double word, word or half word */ static void emit_trunc_r64(struct jit_context *ctx, const u8 dst[], u32 width) { switch (width) { case 64: break; /* Zero-extend a word */ case 32: emit(ctx, move, hi(dst), MIPS_R_ZERO); clobber_reg(ctx, hi(dst)); break; /* Zero-extend a half word */ case 16: emit(ctx, move, hi(dst), MIPS_R_ZERO); emit(ctx, andi, lo(dst), lo(dst), 0xffff); clobber_reg64(ctx, dst); break; } } /* Load operation: dst = *(size*)(src + off) */ static void emit_ldx(struct jit_context *ctx, const u8 dst[], u8 src, s16 off, u8 size) { switch (size) { /* Load a byte */ case BPF_B: emit(ctx, lbu, lo(dst), off, src); emit(ctx, move, hi(dst), MIPS_R_ZERO); break; /* Load a half word */ case BPF_H: emit(ctx, lhu, lo(dst), off, src); emit(ctx, move, hi(dst), MIPS_R_ZERO); break; /* Load a word */ case BPF_W: emit(ctx, lw, lo(dst), off, src); emit(ctx, move, hi(dst), MIPS_R_ZERO); break; /* Load a double word */ case BPF_DW: if (dst[1] == src) { emit(ctx, lw, dst[0], off + 4, src); emit(ctx, lw, dst[1], off, src); } else { emit(ctx, lw, dst[1], off, src); emit(ctx, lw, dst[0], off + 4, src); } emit_load_delay(ctx); break; } clobber_reg64(ctx, dst); } /* Store operation: *(size *)(dst + off) = src */ static void emit_stx(struct jit_context *ctx, const u8 dst, const u8 src[], s16 off, u8 size) { switch (size) { /* Store a byte */ case BPF_B: emit(ctx, sb, lo(src), off, dst); break; /* Store a half word */ case BPF_H: emit(ctx, sh, lo(src), off, dst); break; /* Store a word */ case BPF_W: emit(ctx, sw, lo(src), off, dst); break; /* Store a double word */ case BPF_DW: emit(ctx, sw, src[1], off, dst); emit(ctx, sw, src[0], off + 4, dst); break; } } /* Atomic read-modify-write (32-bit, non-ll/sc fallback) */ static void emit_atomic_r32(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code) { u32 exclude = 0; u32 addr = 0; /* Push caller-saved registers on stack */ push_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, 0, JIT_RESERVED_STACK); /* * Argument 1: dst+off if xchg, otherwise src, passed in register a0 * Argument 2: src if xchg, otherwise dst+off, passed in register a1 */ emit(ctx, move, MIPS_R_T9, dst); if (code == BPF_XCHG) { emit(ctx, move, MIPS_R_A1, src); emit(ctx, addiu, MIPS_R_A0, MIPS_R_T9, off); } else { emit(ctx, move, MIPS_R_A0, src); emit(ctx, addiu, MIPS_R_A1, MIPS_R_T9, off); } /* Emit function call */ switch (code) { case BPF_ADD: addr = (u32)&atomic_add; break; case BPF_ADD | BPF_FETCH: addr = (u32)&atomic_fetch_add; break; case BPF_SUB: addr = (u32)&atomic_sub; break; case BPF_SUB | BPF_FETCH: addr = (u32)&atomic_fetch_sub; break; case BPF_OR: addr = (u32)&atomic_or; break; case BPF_OR | BPF_FETCH: addr = (u32)&atomic_fetch_or; break; case BPF_AND: addr = (u32)&atomic_and; break; case BPF_AND | BPF_FETCH: addr = (u32)&atomic_fetch_and; break; case BPF_XOR: addr = (u32)&atomic_xor; break; case BPF_XOR | BPF_FETCH: addr = (u32)&atomic_fetch_xor; break; case BPF_XCHG: addr = (u32)&atomic_xchg; break; } emit_mov_i(ctx, MIPS_R_T9, addr); emit(ctx, jalr, MIPS_R_RA, MIPS_R_T9); emit(ctx, nop); /* Delay slot */ /* Update src register with old value, if specified */ if (code & BPF_FETCH) { emit(ctx, move, src, MIPS_R_V0); exclude = BIT(src); clobber_reg(ctx, src); } /* Restore caller-saved registers, except any fetched value */ pop_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, exclude, JIT_RESERVED_STACK); emit_load_delay(ctx); clobber_reg(ctx, MIPS_R_RA); } /* Helper function for 64-bit atomic exchange */ static s64 jit_xchg64(s64 a, atomic64_t *v) { return atomic64_xchg(v, a); } /* Atomic read-modify-write (64-bit) */ static void emit_atomic_r64(struct jit_context *ctx, u8 dst, const u8 src[], s16 off, u8 code) { const u8 *r0 = bpf2mips32[BPF_REG_0]; /* Mapped to v0-v1 */ const u8 *r1 = bpf2mips32[BPF_REG_1]; /* Mapped to a0-a1 */ u32 exclude = 0; u32 addr = 0; /* Push caller-saved registers on stack */ push_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, 0, JIT_RESERVED_STACK); /* * Argument 1: 64-bit src, passed in registers a0-a1 * Argument 2: 32-bit dst+off, passed in register a2 */ emit(ctx, move, MIPS_R_T9, dst); emit(ctx, move, r1[0], src[0]); emit(ctx, move, r1[1], src[1]); emit(ctx, addiu, MIPS_R_A2, MIPS_R_T9, off); /* Emit function call */ switch (code) { case BPF_ADD: addr = (u32)&atomic64_add; break; case BPF_ADD | BPF_FETCH: addr = (u32)&atomic64_fetch_add; break; case BPF_SUB: addr = (u32)&atomic64_sub; break; case BPF_SUB | BPF_FETCH: addr = (u32)&atomic64_fetch_sub; break; case BPF_OR: addr = (u32)&atomic64_or; break; case BPF_OR | BPF_FETCH: addr = (u32)&atomic64_fetch_or; break; case BPF_AND: addr = (u32)&atomic64_and; break; case BPF_AND | BPF_FETCH: addr = (u32)&atomic64_fetch_and; break; case BPF_XOR: addr = (u32)&atomic64_xor; break; case BPF_XOR | BPF_FETCH: addr = (u32)&atomic64_fetch_xor; break; case BPF_XCHG: addr = (u32)&jit_xchg64; break; } emit_mov_i(ctx, MIPS_R_T9, addr); emit(ctx, jalr, MIPS_R_RA, MIPS_R_T9); emit(ctx, nop); /* Delay slot */ /* Update src register with old value, if specified */ if (code & BPF_FETCH) { emit(ctx, move, lo(src), lo(r0)); emit(ctx, move, hi(src), hi(r0)); exclude = BIT(src[0]) | BIT(src[1]); clobber_reg64(ctx, src); } /* Restore caller-saved registers, except any fetched value */ pop_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, exclude, JIT_RESERVED_STACK); emit_load_delay(ctx); clobber_reg(ctx, MIPS_R_RA); } /* Atomic compare-and-exchange (32-bit, non-ll/sc fallback) */ static void emit_cmpxchg_r32(struct jit_context *ctx, u8 dst, u8 src, s16 off) { const u8 *r0 = bpf2mips32[BPF_REG_0]; /* Push caller-saved registers on stack */ push_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, JIT_RETURN_REGS, JIT_RESERVED_STACK + 2 * sizeof(u32)); /* * Argument 1: 32-bit dst+off, passed in register a0 * Argument 2: 32-bit r0, passed in register a1 * Argument 3: 32-bit src, passed in register a2 */ emit(ctx, addiu, MIPS_R_T9, dst, off); emit(ctx, move, MIPS_R_T8, src); emit(ctx, move, MIPS_R_A1, lo(r0)); emit(ctx, move, MIPS_R_A0, MIPS_R_T9); emit(ctx, move, MIPS_R_A2, MIPS_R_T8); /* Emit function call */ emit_mov_i(ctx, MIPS_R_T9, (u32)&atomic_cmpxchg); emit(ctx, jalr, MIPS_R_RA, MIPS_R_T9); emit(ctx, nop); /* Delay slot */ #ifdef __BIG_ENDIAN emit(ctx, move, lo(r0), MIPS_R_V0); #endif /* Restore caller-saved registers, except the return value */ pop_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, JIT_RETURN_REGS, JIT_RESERVED_STACK + 2 * sizeof(u32)); emit_load_delay(ctx); clobber_reg(ctx, MIPS_R_V0); clobber_reg(ctx, MIPS_R_V1); clobber_reg(ctx, MIPS_R_RA); } /* Atomic compare-and-exchange (64-bit) */ static void emit_cmpxchg_r64(struct jit_context *ctx, u8 dst, const u8 src[], s16 off) { const u8 *r0 = bpf2mips32[BPF_REG_0]; const u8 *r2 = bpf2mips32[BPF_REG_2]; /* Push caller-saved registers on stack */ push_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, JIT_RETURN_REGS, JIT_RESERVED_STACK + 2 * sizeof(u32)); /* * Argument 1: 32-bit dst+off, passed in register a0 (a1 unused) * Argument 2: 64-bit r0, passed in registers a2-a3 * Argument 3: 64-bit src, passed on stack */ push_regs(ctx, BIT(src[0]) | BIT(src[1]), 0, JIT_RESERVED_STACK); emit(ctx, addiu, MIPS_R_T9, dst, off); emit(ctx, move, r2[0], r0[0]); emit(ctx, move, r2[1], r0[1]); emit(ctx, move, MIPS_R_A0, MIPS_R_T9); /* Emit function call */ emit_mov_i(ctx, MIPS_R_T9, (u32)&atomic64_cmpxchg); emit(ctx, jalr, MIPS_R_RA, MIPS_R_T9); emit(ctx, nop); /* Delay slot */ /* Restore caller-saved registers, except the return value */ pop_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, JIT_RETURN_REGS, JIT_RESERVED_STACK + 2 * sizeof(u32)); emit_load_delay(ctx); clobber_reg(ctx, MIPS_R_V0); clobber_reg(ctx, MIPS_R_V1); clobber_reg(ctx, MIPS_R_RA); } /* * Conditional movz or an emulated equivalent. * Note that the rs register may be modified. */ static void emit_movz_r(struct jit_context *ctx, u8 rd, u8 rs, u8 rt) { if (cpu_has_mips_2) { emit(ctx, movz, rd, rs, rt); /* rd = rt ? rd : rs */ } else if (cpu_has_mips32r6) { if (rs != MIPS_R_ZERO) emit(ctx, seleqz, rs, rs, rt); /* rs = 0 if rt == 0 */ emit(ctx, selnez, rd, rd, rt); /* rd = 0 if rt != 0 */ if (rs != MIPS_R_ZERO) emit(ctx, or, rd, rd, rs); /* rd = rd | rs */ } else { emit(ctx, bnez, rt, 8); /* PC += 8 if rd != 0 */ emit(ctx, nop); /* +0: delay slot */ emit(ctx, or, rd, rs, MIPS_R_ZERO); /* +4: rd = rs */ } clobber_reg(ctx, rd); clobber_reg(ctx, rs); } /* * Conditional movn or an emulated equivalent. * Note that the rs register may be modified. */ static void emit_movn_r(struct jit_context *ctx, u8 rd, u8 rs, u8 rt) { if (cpu_has_mips_2) { emit(ctx, movn, rd, rs, rt); /* rd = rt ? rs : rd */ } else if (cpu_has_mips32r6) { if (rs != MIPS_R_ZERO) emit(ctx, selnez, rs, rs, rt); /* rs = 0 if rt == 0 */ emit(ctx, seleqz, rd, rd, rt); /* rd = 0 if rt != 0 */ if (rs != MIPS_R_ZERO) emit(ctx, or, rd, rd, rs); /* rd = rd | rs */ } else { emit(ctx, beqz, rt, 8); /* PC += 8 if rd == 0 */ emit(ctx, nop); /* +0: delay slot */ emit(ctx, or, rd, rs, MIPS_R_ZERO); /* +4: rd = rs */ } clobber_reg(ctx, rd); clobber_reg(ctx, rs); } /* Emulation of 64-bit sltiu rd, rs, imm, where imm may be S32_MAX + 1 */ static void emit_sltiu_r64(struct jit_context *ctx, u8 rd, const u8 rs[], s64 imm) { u8 tmp = MIPS_R_T9; if (imm < 0) { emit_mov_i(ctx, rd, imm); /* rd = imm */ emit(ctx, sltu, rd, lo(rs), rd); /* rd = rsl < rd */ emit(ctx, sltiu, tmp, hi(rs), -1); /* tmp = rsh < ~0U */ emit(ctx, or, rd, rd, tmp); /* rd = rd | tmp */ } else { /* imm >= 0 */ if (imm > 0x7fff) { emit_mov_i(ctx, rd, (s32)imm); /* rd = imm */ emit(ctx, sltu, rd, lo(rs), rd); /* rd = rsl < rd */ } else { emit(ctx, sltiu, rd, lo(rs), imm); /* rd = rsl < imm */ } emit_movn_r(ctx, rd, MIPS_R_ZERO, hi(rs)); /* rd = 0 if rsh */ } } /* Emulation of 64-bit sltu rd, rs, rt */ static void emit_sltu_r64(struct jit_context *ctx, u8 rd, const u8 rs[], const u8 rt[]) { u8 tmp = MIPS_R_T9; emit(ctx, sltu, rd, lo(rs), lo(rt)); /* rd = rsl < rtl */ emit(ctx, subu, tmp, hi(rs), hi(rt)); /* tmp = rsh - rth */ emit_movn_r(ctx, rd, MIPS_R_ZERO, tmp); /* rd = 0 if tmp != 0 */ emit(ctx, sltu, tmp, hi(rs), hi(rt)); /* tmp = rsh < rth */ emit(ctx, or, rd, rd, tmp); /* rd = rd | tmp */ } /* Emulation of 64-bit slti rd, rs, imm, where imm may be S32_MAX + 1 */ static void emit_slti_r64(struct jit_context *ctx, u8 rd, const u8 rs[], s64 imm) { u8 t1 = MIPS_R_T8; u8 t2 = MIPS_R_T9; u8 cmp; /* * if ((rs < 0) ^ (imm < 0)) t1 = imm >u rsl * else t1 = rsl <u imm */ emit_mov_i(ctx, rd, (s32)imm); emit(ctx, sltu, t1, lo(rs), rd); /* t1 = rsl <u imm */ emit(ctx, sltu, t2, rd, lo(rs)); /* t2 = imm <u rsl */ emit(ctx, srl, rd, hi(rs), 31); /* rd = rsh >> 31 */ if (imm < 0) emit_movz_r(ctx, t1, t2, rd); /* t1 = rd ? t1 : t2 */ else emit_movn_r(ctx, t1, t2, rd); /* t1 = rd ? t2 : t1 */ /* * if ((imm < 0 && rsh != 0xffffffff) || * (imm >= 0 && rsh != 0)) * t1 = 0 */ if (imm < 0) { emit(ctx, addiu, rd, hi(rs), 1); /* rd = rsh + 1 */ cmp = rd; } else { /* imm >= 0 */ cmp = hi(rs); } emit_movn_r(ctx, t1, MIPS_R_ZERO, cmp); /* t1 = 0 if cmp != 0 */ /* * if (imm < 0) rd = rsh < -1 * else rd = rsh != 0 * rd = rd | t1 */ emit(ctx, slti, rd, hi(rs), imm < 0 ? -1 : 0); /* rd = rsh < hi(imm) */ emit(ctx, or, rd, rd, t1); /* rd = rd | t1 */ } /* Emulation of 64-bit(slt rd, rs, rt) */ static void emit_slt_r64(struct jit_context *ctx, u8 rd, const u8 rs[], const u8 rt[]) { u8 t1 = MIPS_R_T7; u8 t2 = MIPS_R_T8; u8 t3 = MIPS_R_T9; /* * if ((rs < 0) ^ (rt < 0)) t1 = rtl <u rsl * else t1 = rsl <u rtl * if (rsh == rth) t1 = 0 */ emit(ctx, sltu, t1, lo(rs), lo(rt)); /* t1 = rsl <u rtl */ emit(ctx, sltu, t2, lo(rt), lo(rs)); /* t2 = rtl <u rsl */ emit(ctx, xor, t3, hi(rs), hi(rt)); /* t3 = rlh ^ rth */ emit(ctx, srl, rd, t3, 31); /* rd = t3 >> 31 */ emit_movn_r(ctx, t1, t2, rd); /* t1 = rd ? t2 : t1 */ emit_movn_r(ctx, t1, MIPS_R_ZERO, t3); /* t1 = 0 if t3 != 0 */ /* rd = (rsh < rth) | t1 */ emit(ctx, slt, rd, hi(rs), hi(rt)); /* rd = rsh <s rth */ emit(ctx, or, rd, rd, t1); /* rd = rd | t1 */ } /* Jump immediate (64-bit) */ static void emit_jmp_i64(struct jit_context *ctx, const u8 dst[], s32 imm, s32 off, u8 op) { u8 tmp = MIPS_R_T6; switch (op) { /* No-op, used internally for branch optimization */ case JIT_JNOP: break; /* PC += off if dst == imm */ /* PC += off if dst != imm */ case BPF_JEQ: case BPF_JNE: if (imm >= -0x7fff && imm <= 0x8000) { emit(ctx, addiu, tmp, lo(dst), -imm); } else if ((u32)imm <= 0xffff) { emit(ctx, xori, tmp, lo(dst), imm); } else { /* Register fallback */ emit_mov_i(ctx, tmp, imm); emit(ctx, xor, tmp, lo(dst), tmp); } if (imm < 0) { /* Compare sign extension */ emit(ctx, addu, MIPS_R_T9, hi(dst), 1); emit(ctx, or, tmp, tmp, MIPS_R_T9); } else { /* Compare zero extension */ emit(ctx, or, tmp, tmp, hi(dst)); } if (op == BPF_JEQ) emit(ctx, beqz, tmp, off); else /* BPF_JNE */ emit(ctx, bnez, tmp, off); break; /* PC += off if dst & imm */ /* PC += off if (dst & imm) == 0 (not in BPF, used for long jumps) */ case BPF_JSET: case JIT_JNSET: if ((u32)imm <= 0xffff) { emit(ctx, andi, tmp, lo(dst), imm); } else { /* Register fallback */ emit_mov_i(ctx, tmp, imm); emit(ctx, and, tmp, lo(dst), tmp); } if (imm < 0) /* Sign-extension pulls in high word */ emit(ctx, or, tmp, tmp, hi(dst)); if (op == BPF_JSET) emit(ctx, bnez, tmp, off); else /* JIT_JNSET */ emit(ctx, beqz, tmp, off); break; /* PC += off if dst > imm */ case BPF_JGT: emit_sltiu_r64(ctx, tmp, dst, (s64)imm + 1); emit(ctx, beqz, tmp, off); break; /* PC += off if dst >= imm */ case BPF_JGE: emit_sltiu_r64(ctx, tmp, dst, imm); emit(ctx, beqz, tmp, off); break; /* PC += off if dst < imm */ case BPF_JLT: emit_sltiu_r64(ctx, tmp, dst, imm); emit(ctx, bnez, tmp, off); break; /* PC += off if dst <= imm */ case BPF_JLE: emit_sltiu_r64(ctx, tmp, dst, (s64)imm + 1); emit(ctx, bnez, tmp, off); break; /* PC += off if dst > imm (signed) */ case BPF_JSGT: emit_slti_r64(ctx, tmp, dst, (s64)imm + 1); emit(ctx, beqz, tmp, off); break; /* PC += off if dst >= imm (signed) */ case BPF_JSGE: emit_slti_r64(ctx, tmp, dst, imm); emit(ctx, beqz, tmp, off); break; /* PC += off if dst < imm (signed) */ case BPF_JSLT: emit_slti_r64(ctx, tmp, dst, imm); emit(ctx, bnez, tmp, off); break; /* PC += off if dst <= imm (signed) */ case BPF_JSLE: emit_slti_r64(ctx, tmp, dst, (s64)imm + 1); emit(ctx, bnez, tmp, off); break; } } /* Jump register (64-bit) */ static void emit_jmp_r64(struct jit_context *ctx, const u8 dst[], const u8 src[], s32 off, u8 op) { u8 t1 = MIPS_R_T6; u8 t2 = MIPS_R_T7; switch (op) { /* No-op, used internally for branch optimization */ case JIT_JNOP: break; /* PC += off if dst == src */ /* PC += off if dst != src */ case BPF_JEQ: case BPF_JNE: emit(ctx, subu, t1, lo(dst), lo(src)); emit(ctx, subu, t2, hi(dst), hi(src)); emit(ctx, or, t1, t1, t2); if (op == BPF_JEQ) emit(ctx, beqz, t1, off); else /* BPF_JNE */ emit(ctx, bnez, t1, off); break; /* PC += off if dst & src */ /* PC += off if (dst & imm) == 0 (not in BPF, used for long jumps) */ case BPF_JSET: case JIT_JNSET: emit(ctx, and, t1, lo(dst), lo(src)); emit(ctx, and, t2, hi(dst), hi(src)); emit(ctx, or, t1, t1, t2); if (op == BPF_JSET) emit(ctx, bnez, t1, off); else /* JIT_JNSET */ emit(ctx, beqz, t1, off); break; /* PC += off if dst > src */ case BPF_JGT: emit_sltu_r64(ctx, t1, src, dst); emit(ctx, bnez, t1, off); break; /* PC += off if dst >= src */ case BPF_JGE: emit_sltu_r64(ctx, t1, dst, src); emit(ctx, beqz, t1, off); break; /* PC += off if dst < src */ case BPF_JLT: emit_sltu_r64(ctx, t1, dst, src); emit(ctx, bnez, t1, off); break; /* PC += off if dst <= src */ case BPF_JLE: emit_sltu_r64(ctx, t1, src, dst); emit(ctx, beqz, t1, off); break; /* PC += off if dst > src (signed) */ case BPF_JSGT: emit_slt_r64(ctx, t1, src, dst); emit(ctx, bnez, t1, off); break; /* PC += off if dst >= src (signed) */ case BPF_JSGE: emit_slt_r64(ctx, t1, dst, src); emit(ctx, beqz, t1, off); break; /* PC += off if dst < src (signed) */ case BPF_JSLT: emit_slt_r64(ctx, t1, dst, src); emit(ctx, bnez, t1, off); break; /* PC += off if dst <= src (signed) */ case BPF_JSLE: emit_slt_r64(ctx, t1, src, dst); emit(ctx, beqz, t1, off); break; } } /* Function call */ static int emit_call(struct jit_context *ctx, const struct bpf_insn *insn) { bool fixed; u64 addr; /* Decode the call address */ if (bpf_jit_get_func_addr(ctx->program, insn, false, &addr, &fixed) < 0) return -1; if (!fixed) return -1; /* Push stack arguments */ push_regs(ctx, JIT_STACK_REGS, 0, JIT_RESERVED_STACK); /* Emit function call */ emit_mov_i(ctx, MIPS_R_T9, addr); emit(ctx, jalr, MIPS_R_RA, MIPS_R_T9); emit(ctx, nop); /* Delay slot */ clobber_reg(ctx, MIPS_R_RA); clobber_reg(ctx, MIPS_R_V0); clobber_reg(ctx, MIPS_R_V1); return 0; } /* Function tail call */ static int emit_tail_call(struct jit_context *ctx) { u8 ary = lo(bpf2mips32[BPF_REG_2]); u8 ind = lo(bpf2mips32[BPF_REG_3]); u8 t1 = MIPS_R_T8; u8 t2 = MIPS_R_T9; int off; /* * Tail call: * eBPF R1 - function argument (context ptr), passed in a0-a1 * eBPF R2 - ptr to object with array of function entry points * eBPF R3 - array index of function to be called * stack[sz] - remaining tail call count, initialized in prologue */ /* if (ind >= ary->map.max_entries) goto out */ off = offsetof(struct bpf_array, map.max_entries); if (off > 0x7fff) return -1; emit(ctx, lw, t1, off, ary); /* t1 = ary->map.max_entries*/ emit_load_delay(ctx); /* Load delay slot */ emit(ctx, sltu, t1, ind, t1); /* t1 = ind < t1 */ emit(ctx, beqz, t1, get_offset(ctx, 1)); /* PC += off(1) if t1 == 0 */ /* (next insn delay slot) */ /* if (TCC-- <= 0) goto out */ emit(ctx, lw, t2, ctx->stack_size, MIPS_R_SP); /* t2 = *(SP + size) */ emit_load_delay(ctx); /* Load delay slot */ emit(ctx, blez, t2, get_offset(ctx, 1)); /* PC += off(1) if t2 <= 0 */ emit(ctx, addiu, t2, t2, -1); /* t2-- (delay slot) */ emit(ctx, sw, t2, ctx->stack_size, MIPS_R_SP); /* *(SP + size) = t2 */ /* prog = ary->ptrs[ind] */ off = offsetof(struct bpf_array, ptrs); if (off > 0x7fff) return -1; emit(ctx, sll, t1, ind, 2); /* t1 = ind << 2 */ emit(ctx, addu, t1, t1, ary); /* t1 += ary */ emit(ctx, lw, t2, off, t1); /* t2 = *(t1 + off) */ emit_load_delay(ctx); /* Load delay slot */ /* if (prog == 0) goto out */ emit(ctx, beqz, t2, get_offset(ctx, 1)); /* PC += off(1) if t2 == 0 */ emit(ctx, nop); /* Delay slot */ /* func = prog->bpf_func + 8 (prologue skip offset) */ off = offsetof(struct bpf_prog, bpf_func); if (off > 0x7fff) return -1; emit(ctx, lw, t1, off, t2); /* t1 = *(t2 + off) */ emit_load_delay(ctx); /* Load delay slot */ emit(ctx, addiu, t1, t1, JIT_TCALL_SKIP); /* t1 += skip (8 or 12) */ /* goto func */ build_epilogue(ctx, t1); return 0; } /* * Stack frame layout for a JITed program (stack grows down). * * Higher address : Caller's stack frame : * :----------------------------: * : 64-bit eBPF args r3-r5 : * :----------------------------: * : Reserved / tail call count : * +============================+ <--- MIPS sp before call * | Callee-saved registers, | * | including RA and FP | * +----------------------------+ <--- eBPF FP (MIPS zero,fp) * | Local eBPF variables | * | allocated by program | * +----------------------------+ * | Reserved for caller-saved | * | registers | * +----------------------------+ * | Reserved for 64-bit eBPF | * | args r3-r5 & args passed | * | on stack in kernel calls | * Lower address +============================+ <--- MIPS sp */ /* Build program prologue to set up the stack and registers */ void build_prologue(struct jit_context *ctx) { const u8 *r1 = bpf2mips32[BPF_REG_1]; const u8 *fp = bpf2mips32[BPF_REG_FP]; int stack, saved, locals, reserved; /* * In the unlikely event that the TCC limit is raised to more * than 16 bits, it is clamped to the maximum value allowed for * the generated code (0xffff). It is better fail to compile * instead of degrading gracefully. */ BUILD_BUG_ON(MAX_TAIL_CALL_CNT > 0xffff); /* * The first two instructions initialize TCC in the reserved (for us) * 16-byte area in the parent's stack frame. On a tail call, the * calling function jumps into the prologue after these instructions. */ emit(ctx, ori, MIPS_R_T9, MIPS_R_ZERO, MAX_TAIL_CALL_CNT); emit(ctx, sw, MIPS_R_T9, 0, MIPS_R_SP); /* * Register eBPF R1 contains the 32-bit context pointer argument. * A 32-bit argument is always passed in MIPS register a0, regardless * of CPU endianness. Initialize R1 accordingly and zero-extend. */ #ifdef __BIG_ENDIAN emit(ctx, move, lo(r1), MIPS_R_A0); #endif /* === Entry-point for tail calls === */ /* Zero-extend the 32-bit argument */ emit(ctx, move, hi(r1), MIPS_R_ZERO); /* If the eBPF frame pointer was accessed it must be saved */ if (ctx->accessed & BIT(BPF_REG_FP)) clobber_reg64(ctx, fp); /* Compute the stack space needed for callee-saved registers */ saved = hweight32(ctx->clobbered & JIT_CALLEE_REGS) * sizeof(u32); saved = ALIGN(saved, MIPS_STACK_ALIGNMENT); /* Stack space used by eBPF program local data */ locals = ALIGN(ctx->program->aux->stack_depth, MIPS_STACK_ALIGNMENT); /* * If we are emitting function calls, reserve extra stack space for * caller-saved registers and function arguments passed on the stack. * The required space is computed automatically during resource * usage discovery (pass 1). */ reserved = ctx->stack_used; /* Allocate the stack frame */ stack = ALIGN(saved + locals + reserved, MIPS_STACK_ALIGNMENT); emit(ctx, addiu, MIPS_R_SP, MIPS_R_SP, -stack); /* Store callee-saved registers on stack */ push_regs(ctx, ctx->clobbered & JIT_CALLEE_REGS, 0, stack - saved); /* Initialize the eBPF frame pointer if accessed */ if (ctx->accessed & BIT(BPF_REG_FP)) emit(ctx, addiu, lo(fp), MIPS_R_SP, stack - saved); ctx->saved_size = saved; ctx->stack_size = stack; } /* Build the program epilogue to restore the stack and registers */ void build_epilogue(struct jit_context *ctx, int dest_reg) { /* Restore callee-saved registers from stack */ pop_regs(ctx, ctx->clobbered & JIT_CALLEE_REGS, 0, ctx->stack_size - ctx->saved_size); /* * A 32-bit return value is always passed in MIPS register v0, * but on big-endian targets the low part of R0 is mapped to v1. */ #ifdef __BIG_ENDIAN emit(ctx, move, MIPS_R_V0, MIPS_R_V1); #endif /* Jump to the return address and adjust the stack pointer */ emit(ctx, jr, dest_reg); emit(ctx, addiu, MIPS_R_SP, MIPS_R_SP, ctx->stack_size); } /* Build one eBPF instruction */ int build_insn(const struct bpf_insn *insn, struct jit_context *ctx) { const u8 *dst = bpf2mips32[insn->dst_reg]; const u8 *src = bpf2mips32[insn->src_reg]; const u8 *res = bpf2mips32[BPF_REG_0]; const u8 *tmp = bpf2mips32[JIT_REG_TMP]; u8 code = insn->code; s16 off = insn->off; s32 imm = insn->imm; s32 val, rel; u8 alu, jmp; switch (code) { /* ALU operations */ /* dst = imm */ case BPF_ALU | BPF_MOV | BPF_K: emit_mov_i(ctx, lo(dst), imm); emit_zext_ver(ctx, dst); break; /* dst = src */ case BPF_ALU | BPF_MOV | BPF_X: if (imm == 1) { /* Special mov32 for zext */ emit_mov_i(ctx, hi(dst), 0); } else { emit_mov_r(ctx, lo(dst), lo(src)); emit_zext_ver(ctx, dst); } break; /* dst = -dst */ case BPF_ALU | BPF_NEG: emit_alu_i(ctx, lo(dst), 0, BPF_NEG); emit_zext_ver(ctx, dst); break; /* dst = dst & imm */ /* dst = dst | imm */ /* dst = dst ^ imm */ /* dst = dst << imm */ /* dst = dst >> imm */ /* dst = dst >> imm (arithmetic) */ /* dst = dst + imm */ /* dst = dst - imm */ /* dst = dst * imm */ /* dst = dst / imm */ /* dst = dst % imm */ case BPF_ALU | BPF_OR | BPF_K: case BPF_ALU | BPF_AND | BPF_K: case BPF_ALU | BPF_XOR | BPF_K: case BPF_ALU | BPF_LSH | BPF_K: case BPF_ALU | BPF_RSH | BPF_K: case BPF_ALU | BPF_ARSH | BPF_K: case BPF_ALU | BPF_ADD | BPF_K: case BPF_ALU | BPF_SUB | BPF_K: case BPF_ALU | BPF_MUL | BPF_K: case BPF_ALU | BPF_DIV | BPF_K: case BPF_ALU | BPF_MOD | BPF_K: if (!valid_alu_i(BPF_OP(code), imm)) { emit_mov_i(ctx, MIPS_R_T6, imm); emit_alu_r(ctx, lo(dst), MIPS_R_T6, BPF_OP(code)); } else if (rewrite_alu_i(BPF_OP(code), imm, &alu, &val)) { emit_alu_i(ctx, lo(dst), val, alu); } emit_zext_ver(ctx, dst); break; /* dst = dst & src */ /* dst = dst | src */ /* dst = dst ^ src */ /* dst = dst << src */ /* dst = dst >> src */ /* dst = dst >> src (arithmetic) */ /* dst = dst + src */ /* dst = dst - src */ /* dst = dst * src */ /* dst = dst / src */ /* dst = dst % src */ case BPF_ALU | BPF_AND | BPF_X: case BPF_ALU | BPF_OR | BPF_X: case BPF_ALU | BPF_XOR | BPF_X: case BPF_ALU | BPF_LSH | BPF_X: case BPF_ALU | BPF_RSH | BPF_X: case BPF_ALU | BPF_ARSH | BPF_X: case BPF_ALU | BPF_ADD | BPF_X: case BPF_ALU | BPF_SUB | BPF_X: case BPF_ALU | BPF_MUL | BPF_X: case BPF_ALU | BPF_DIV | BPF_X: case BPF_ALU | BPF_MOD | BPF_X: emit_alu_r(ctx, lo(dst), lo(src), BPF_OP(code)); emit_zext_ver(ctx, dst); break; /* dst = imm (64-bit) */ case BPF_ALU64 | BPF_MOV | BPF_K: emit_mov_se_i64(ctx, dst, imm); break; /* dst = src (64-bit) */ case BPF_ALU64 | BPF_MOV | BPF_X: emit_mov_r(ctx, lo(dst), lo(src)); emit_mov_r(ctx, hi(dst), hi(src)); break; /* dst = -dst (64-bit) */ case BPF_ALU64 | BPF_NEG: emit_neg_i64(ctx, dst); break; /* dst = dst & imm (64-bit) */ case BPF_ALU64 | BPF_AND | BPF_K: emit_alu_i64(ctx, dst, imm, BPF_OP(code)); break; /* dst = dst | imm (64-bit) */ /* dst = dst ^ imm (64-bit) */ /* dst = dst + imm (64-bit) */ /* dst = dst - imm (64-bit) */ case BPF_ALU64 | BPF_OR | BPF_K: case BPF_ALU64 | BPF_XOR | BPF_K: case BPF_ALU64 | BPF_ADD | BPF_K: case BPF_ALU64 | BPF_SUB | BPF_K: if (imm) emit_alu_i64(ctx, dst, imm, BPF_OP(code)); break; /* dst = dst << imm (64-bit) */ /* dst = dst >> imm (64-bit) */ /* dst = dst >> imm (64-bit, arithmetic) */ case BPF_ALU64 | BPF_LSH | BPF_K: case BPF_ALU64 | BPF_RSH | BPF_K: case BPF_ALU64 | BPF_ARSH | BPF_K: if (imm) emit_shift_i64(ctx, dst, imm, BPF_OP(code)); break; /* dst = dst * imm (64-bit) */ case BPF_ALU64 | BPF_MUL | BPF_K: emit_mul_i64(ctx, dst, imm); break; /* dst = dst / imm (64-bit) */ /* dst = dst % imm (64-bit) */ case BPF_ALU64 | BPF_DIV | BPF_K: case BPF_ALU64 | BPF_MOD | BPF_K: /* * Sign-extend the immediate value into a temporary register, * and then do the operation on this register. */ emit_mov_se_i64(ctx, tmp, imm); emit_divmod_r64(ctx, dst, tmp, BPF_OP(code)); break; /* dst = dst & src (64-bit) */ /* dst = dst | src (64-bit) */ /* dst = dst ^ src (64-bit) */ /* dst = dst + src (64-bit) */ /* dst = dst - src (64-bit) */ case BPF_ALU64 | BPF_AND | BPF_X: case BPF_ALU64 | BPF_OR | BPF_X: case BPF_ALU64 | BPF_XOR | BPF_X: case BPF_ALU64 | BPF_ADD | BPF_X: case BPF_ALU64 | BPF_SUB | BPF_X: emit_alu_r64(ctx, dst, src, BPF_OP(code)); break; /* dst = dst << src (64-bit) */ /* dst = dst >> src (64-bit) */ /* dst = dst >> src (64-bit, arithmetic) */ case BPF_ALU64 | BPF_LSH | BPF_X: case BPF_ALU64 | BPF_RSH | BPF_X: case BPF_ALU64 | BPF_ARSH | BPF_X: emit_shift_r64(ctx, dst, lo(src), BPF_OP(code)); break; /* dst = dst * src (64-bit) */ case BPF_ALU64 | BPF_MUL | BPF_X: emit_mul_r64(ctx, dst, src); break; /* dst = dst / src (64-bit) */ /* dst = dst % src (64-bit) */ case BPF_ALU64 | BPF_DIV | BPF_X: case BPF_ALU64 | BPF_MOD | BPF_X: emit_divmod_r64(ctx, dst, src, BPF_OP(code)); break; /* dst = htole(dst) */ /* dst = htobe(dst) */ case BPF_ALU | BPF_END | BPF_FROM_LE: case BPF_ALU | BPF_END | BPF_FROM_BE: if (BPF_SRC(code) == #ifdef __BIG_ENDIAN BPF_FROM_LE #else BPF_FROM_BE #endif ) emit_bswap_r64(ctx, dst, imm); else emit_trunc_r64(ctx, dst, imm); break; /* dst = imm64 */ case BPF_LD | BPF_IMM | BPF_DW: emit_mov_i(ctx, lo(dst), imm); emit_mov_i(ctx, hi(dst), insn[1].imm); return 1; /* LDX: dst = *(size *)(src + off) */ case BPF_LDX | BPF_MEM | BPF_W: case BPF_LDX | BPF_MEM | BPF_H: case BPF_LDX | BPF_MEM | BPF_B: case BPF_LDX | BPF_MEM | BPF_DW: emit_ldx(ctx, dst, lo(src), off, BPF_SIZE(code)); break; /* ST: *(size *)(dst + off) = imm */ case BPF_ST | BPF_MEM | BPF_W: case BPF_ST | BPF_MEM | BPF_H: case BPF_ST | BPF_MEM | BPF_B: case BPF_ST | BPF_MEM | BPF_DW: switch (BPF_SIZE(code)) { case BPF_DW: /* Sign-extend immediate value into temporary reg */ emit_mov_se_i64(ctx, tmp, imm); break; case BPF_W: case BPF_H: case BPF_B: emit_mov_i(ctx, lo(tmp), imm); break; } emit_stx(ctx, lo(dst), tmp, off, BPF_SIZE(code)); break; /* STX: *(size *)(dst + off) = src */ case BPF_STX | BPF_MEM | BPF_W: case BPF_STX | BPF_MEM | BPF_H: case BPF_STX | BPF_MEM | BPF_B: case BPF_STX | BPF_MEM | BPF_DW: emit_stx(ctx, lo(dst), src, off, BPF_SIZE(code)); break; /* Speculation barrier */ case BPF_ST | BPF_NOSPEC: break; /* Atomics */ case BPF_STX | BPF_ATOMIC | BPF_W: switch (imm) { case BPF_ADD: case BPF_ADD | BPF_FETCH: case BPF_AND: case BPF_AND | BPF_FETCH: case BPF_OR: case BPF_OR | BPF_FETCH: case BPF_XOR: case BPF_XOR | BPF_FETCH: case BPF_XCHG: if (cpu_has_llsc) emit_atomic_r(ctx, lo(dst), lo(src), off, imm); else /* Non-ll/sc fallback */ emit_atomic_r32(ctx, lo(dst), lo(src), off, imm); if (imm & BPF_FETCH) emit_zext_ver(ctx, src); break; case BPF_CMPXCHG: if (cpu_has_llsc) emit_cmpxchg_r(ctx, lo(dst), lo(src), lo(res), off); else /* Non-ll/sc fallback */ emit_cmpxchg_r32(ctx, lo(dst), lo(src), off); /* Result zero-extension inserted by verifier */ break; default: goto notyet; } break; /* Atomics (64-bit) */ case BPF_STX | BPF_ATOMIC | BPF_DW: switch (imm) { case BPF_ADD: case BPF_ADD | BPF_FETCH: case BPF_AND: case BPF_AND | BPF_FETCH: case BPF_OR: case BPF_OR | BPF_FETCH: case BPF_XOR: case BPF_XOR | BPF_FETCH: case BPF_XCHG: emit_atomic_r64(ctx, lo(dst), src, off, imm); break; case BPF_CMPXCHG: emit_cmpxchg_r64(ctx, lo(dst), src, off); break; default: goto notyet; } break; /* PC += off if dst == src */ /* PC += off if dst != src */ /* PC += off if dst & src */ /* PC += off if dst > src */ /* PC += off if dst >= src */ /* PC += off if dst < src */ /* PC += off if dst <= src */ /* PC += off if dst > src (signed) */ /* PC += off if dst >= src (signed) */ /* PC += off if dst < src (signed) */ /* PC += off if dst <= src (signed) */ case BPF_JMP32 | BPF_JEQ | BPF_X: case BPF_JMP32 | BPF_JNE | BPF_X: case BPF_JMP32 | BPF_JSET | BPF_X: case BPF_JMP32 | BPF_JGT | BPF_X: case BPF_JMP32 | BPF_JGE | BPF_X: case BPF_JMP32 | BPF_JLT | BPF_X: case BPF_JMP32 | BPF_JLE | BPF_X: case BPF_JMP32 | BPF_JSGT | BPF_X: case BPF_JMP32 | BPF_JSGE | BPF_X: case BPF_JMP32 | BPF_JSLT | BPF_X: case BPF_JMP32 | BPF_JSLE | BPF_X: if (off == 0) break; setup_jmp_r(ctx, dst == src, BPF_OP(code), off, &jmp, &rel); emit_jmp_r(ctx, lo(dst), lo(src), rel, jmp); if (finish_jmp(ctx, jmp, off) < 0) goto toofar; break; /* PC += off if dst == imm */ /* PC += off if dst != imm */ /* PC += off if dst & imm */ /* PC += off if dst > imm */ /* PC += off if dst >= imm */ /* PC += off if dst < imm */ /* PC += off if dst <= imm */ /* PC += off if dst > imm (signed) */ /* PC += off if dst >= imm (signed) */ /* PC += off if dst < imm (signed) */ /* PC += off if dst <= imm (signed) */ case BPF_JMP32 | BPF_JEQ | BPF_K: case BPF_JMP32 | BPF_JNE | BPF_K: case BPF_JMP32 | BPF_JSET | BPF_K: case BPF_JMP32 | BPF_JGT | BPF_K: case BPF_JMP32 | BPF_JGE | BPF_K: case BPF_JMP32 | BPF_JLT | BPF_K: case BPF_JMP32 | BPF_JLE | BPF_K: case BPF_JMP32 | BPF_JSGT | BPF_K: case BPF_JMP32 | BPF_JSGE | BPF_K: case BPF_JMP32 | BPF_JSLT | BPF_K: case BPF_JMP32 | BPF_JSLE | BPF_K: if (off == 0) break; setup_jmp_i(ctx, imm, 32, BPF_OP(code), off, &jmp, &rel); if (valid_jmp_i(jmp, imm)) { emit_jmp_i(ctx, lo(dst), imm, rel, jmp); } else { /* Move large immediate to register */ emit_mov_i(ctx, MIPS_R_T6, imm); emit_jmp_r(ctx, lo(dst), MIPS_R_T6, rel, jmp); } if (finish_jmp(ctx, jmp, off) < 0) goto toofar; break; /* PC += off if dst == src */ /* PC += off if dst != src */ /* PC += off if dst & src */ /* PC += off if dst > src */ /* PC += off if dst >= src */ /* PC += off if dst < src */ /* PC += off if dst <= src */ /* PC += off if dst > src (signed) */ /* PC += off if dst >= src (signed) */ /* PC += off if dst < src (signed) */ /* PC += off if dst <= src (signed) */ case BPF_JMP | BPF_JEQ | BPF_X: case BPF_JMP | BPF_JNE | BPF_X: case BPF_JMP | BPF_JSET | BPF_X: case BPF_JMP | BPF_JGT | BPF_X: case BPF_JMP | BPF_JGE | BPF_X: case BPF_JMP | BPF_JLT | BPF_X: case BPF_JMP | BPF_JLE | BPF_X: case BPF_JMP | BPF_JSGT | BPF_X: case BPF_JMP | BPF_JSGE | BPF_X: case BPF_JMP | BPF_JSLT | BPF_X: case BPF_JMP | BPF_JSLE | BPF_X: if (off == 0) break; setup_jmp_r(ctx, dst == src, BPF_OP(code), off, &jmp, &rel); emit_jmp_r64(ctx, dst, src, rel, jmp); if (finish_jmp(ctx, jmp, off) < 0) goto toofar; break; /* PC += off if dst == imm */ /* PC += off if dst != imm */ /* PC += off if dst & imm */ /* PC += off if dst > imm */ /* PC += off if dst >= imm */ /* PC += off if dst < imm */ /* PC += off if dst <= imm */ /* PC += off if dst > imm (signed) */ /* PC += off if dst >= imm (signed) */ /* PC += off if dst < imm (signed) */ /* PC += off if dst <= imm (signed) */ case BPF_JMP | BPF_JEQ | BPF_K: case BPF_JMP | BPF_JNE | BPF_K: case BPF_JMP | BPF_JSET | BPF_K: case BPF_JMP | BPF_JGT | BPF_K: case BPF_JMP | BPF_JGE | BPF_K: case BPF_JMP | BPF_JLT | BPF_K: case BPF_JMP | BPF_JLE | BPF_K: case BPF_JMP | BPF_JSGT | BPF_K: case BPF_JMP | BPF_JSGE | BPF_K: case BPF_JMP | BPF_JSLT | BPF_K: case BPF_JMP | BPF_JSLE | BPF_K: if (off == 0) break; setup_jmp_i(ctx, imm, 64, BPF_OP(code), off, &jmp, &rel); emit_jmp_i64(ctx, dst, imm, rel, jmp); if (finish_jmp(ctx, jmp, off) < 0) goto toofar; break; /* PC += off */ case BPF_JMP | BPF_JA: if (off == 0) break; if (emit_ja(ctx, off) < 0) goto toofar; break; /* Tail call */ case BPF_JMP | BPF_TAIL_CALL: if (emit_tail_call(ctx) < 0) goto invalid; break; /* Function call */ case BPF_JMP | BPF_CALL: if (emit_call(ctx, insn) < 0) goto invalid; break; /* Function return */ case BPF_JMP | BPF_EXIT: /* * Optimization: when last instruction is EXIT * simply continue to epilogue. */ if (ctx->bpf_index == ctx->program->len - 1) break; if (emit_exit(ctx) < 0) goto toofar; break; default: invalid: pr_err_once("unknown opcode %02x\n", code); return -EINVAL; notyet: pr_info_once("*** NOT YET: opcode %02x ***\n", code); return -EFAULT; toofar: pr_info_once("*** TOO FAR: jump at %u opcode %02x ***\n", ctx->bpf_index, code); return -E2BIG; } return 0; }
linux-master
arch/mips/net/bpf_jit_comp32.c
/* * Toshiba RBTX4927 specific interrupt handlers * * Author: MontaVista Software, Inc. * [email protected] * * Copyright 2001-2002 MontaVista Software Inc. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */ /* * I8259A_IRQ_BASE+00 * I8259A_IRQ_BASE+01 PS2/Keyboard * I8259A_IRQ_BASE+02 Cascade RBTX4927-ISA (irqs 8-15) * I8259A_IRQ_BASE+03 * I8259A_IRQ_BASE+04 * I8259A_IRQ_BASE+05 * I8259A_IRQ_BASE+06 * I8259A_IRQ_BASE+07 * I8259A_IRQ_BASE+08 * I8259A_IRQ_BASE+09 * I8259A_IRQ_BASE+10 * I8259A_IRQ_BASE+11 * I8259A_IRQ_BASE+12 PS2/Mouse (not supported at this time) * I8259A_IRQ_BASE+13 * I8259A_IRQ_BASE+14 IDE * I8259A_IRQ_BASE+15 * * MIPS_CPU_IRQ_BASE+00 Software 0 * MIPS_CPU_IRQ_BASE+01 Software 1 * MIPS_CPU_IRQ_BASE+02 Cascade TX4927-CP0 * MIPS_CPU_IRQ_BASE+03 Multiplexed -- do not use * MIPS_CPU_IRQ_BASE+04 Multiplexed -- do not use * MIPS_CPU_IRQ_BASE+05 Multiplexed -- do not use * MIPS_CPU_IRQ_BASE+06 Multiplexed -- do not use * MIPS_CPU_IRQ_BASE+07 CPU TIMER * * TXX9_IRQ_BASE+00 * TXX9_IRQ_BASE+01 * TXX9_IRQ_BASE+02 * TXX9_IRQ_BASE+03 Cascade RBTX4927-IOC * TXX9_IRQ_BASE+04 * TXX9_IRQ_BASE+05 RBTX4927 RTL-8019AS ethernet * TXX9_IRQ_BASE+06 * TXX9_IRQ_BASE+07 * TXX9_IRQ_BASE+08 TX4927 SerialIO Channel 0 * TXX9_IRQ_BASE+09 TX4927 SerialIO Channel 1 * TXX9_IRQ_BASE+10 * TXX9_IRQ_BASE+11 * TXX9_IRQ_BASE+12 * TXX9_IRQ_BASE+13 * TXX9_IRQ_BASE+14 * TXX9_IRQ_BASE+15 * TXX9_IRQ_BASE+16 TX4927 PCI PCI-C * TXX9_IRQ_BASE+17 * TXX9_IRQ_BASE+18 * TXX9_IRQ_BASE+19 * TXX9_IRQ_BASE+20 * TXX9_IRQ_BASE+21 * TXX9_IRQ_BASE+22 TX4927 PCI PCI-ERR * TXX9_IRQ_BASE+23 TX4927 PCI PCI-PMA (not used) * TXX9_IRQ_BASE+24 * TXX9_IRQ_BASE+25 * TXX9_IRQ_BASE+26 * TXX9_IRQ_BASE+27 * TXX9_IRQ_BASE+28 * TXX9_IRQ_BASE+29 * TXX9_IRQ_BASE+30 * TXX9_IRQ_BASE+31 * * RBTX4927_IRQ_IOC+00 FPCIB0 PCI-D (SouthBridge) * RBTX4927_IRQ_IOC+01 FPCIB0 PCI-C (SouthBridge) * RBTX4927_IRQ_IOC+02 FPCIB0 PCI-B (SouthBridge/IDE/pin=1,INTR) * RBTX4927_IRQ_IOC+03 FPCIB0 PCI-A (SouthBridge/USB/pin=4) * RBTX4927_IRQ_IOC+04 * RBTX4927_IRQ_IOC+05 * RBTX4927_IRQ_IOC+06 * RBTX4927_IRQ_IOC+07 * * NOTES: * SouthBridge/INTR is mapped to SouthBridge/A=PCI-B/#58 * SouthBridge/ISA/pin=0 no pci irq used by this device * SouthBridge/IDE/pin=1 no pci irq used by this device, using INTR * via ISA IRQ14 * SouthBridge/USB/pin=4 using pci irq SouthBridge/D=PCI-A=#59 * SouthBridge/PMC/pin=0 no pci irq used by this device * SuperIO/PS2/Keyboard, using INTR via ISA IRQ1 * SuperIO/PS2/Mouse, using INTR via ISA IRQ12 (mouse not currently supported) * JP7 is not bus master -- do NOT use -- only 4 pci bus master's * allowed -- SouthBridge, JP4, JP5, JP6 */ #include <linux/init.h> #include <linux/types.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <asm/io.h> #include <asm/mipsregs.h> #include <asm/txx9/generic.h> #include <asm/txx9/rbtx4927.h> static int toshiba_rbtx4927_irq_nested(int sw_irq) { u8 level3; level3 = readb(rbtx4927_imstat_addr) & 0x1f; if (unlikely(!level3)) return -1; return RBTX4927_IRQ_IOC + __fls8(level3); } static void toshiba_rbtx4927_irq_ioc_enable(struct irq_data *d) { unsigned char v; v = readb(rbtx4927_imask_addr); v |= (1 << (d->irq - RBTX4927_IRQ_IOC)); writeb(v, rbtx4927_imask_addr); } static void toshiba_rbtx4927_irq_ioc_disable(struct irq_data *d) { unsigned char v; v = readb(rbtx4927_imask_addr); v &= ~(1 << (d->irq - RBTX4927_IRQ_IOC)); writeb(v, rbtx4927_imask_addr); mmiowb(); } #define TOSHIBA_RBTX4927_IOC_NAME "RBTX4927-IOC" static struct irq_chip toshiba_rbtx4927_irq_ioc_type = { .name = TOSHIBA_RBTX4927_IOC_NAME, .irq_mask = toshiba_rbtx4927_irq_ioc_disable, .irq_unmask = toshiba_rbtx4927_irq_ioc_enable, }; static void __init toshiba_rbtx4927_irq_ioc_init(void) { int i; /* mask all IOC interrupts */ writeb(0, rbtx4927_imask_addr); /* clear SoftInt interrupts */ writeb(0, rbtx4927_softint_addr); for (i = RBTX4927_IRQ_IOC; i < RBTX4927_IRQ_IOC + RBTX4927_NR_IRQ_IOC; i++) irq_set_chip_and_handler(i, &toshiba_rbtx4927_irq_ioc_type, handle_level_irq); irq_set_chained_handler(RBTX4927_IRQ_IOCINT, handle_simple_irq); } static int rbtx4927_irq_dispatch(int pending) { int irq; if (pending & STATUSF_IP7) /* cpu timer */ irq = MIPS_CPU_IRQ_BASE + 7; else if (pending & STATUSF_IP2) { /* tx4927 pic */ irq = txx9_irq(); if (irq == RBTX4927_IRQ_IOCINT) irq = toshiba_rbtx4927_irq_nested(irq); } else if (pending & STATUSF_IP0) /* user line 0 */ irq = MIPS_CPU_IRQ_BASE + 0; else if (pending & STATUSF_IP1) /* user line 1 */ irq = MIPS_CPU_IRQ_BASE + 1; else irq = -1; return irq; } void __init rbtx4927_irq_setup(void) { txx9_irq_dispatch = rbtx4927_irq_dispatch; tx4927_irq_init(); toshiba_rbtx4927_irq_ioc_init(); /* Onboard 10M Ether: High Active */ irq_set_irq_type(RBTX4927_RTL_8019_IRQ, IRQF_TRIGGER_HIGH); }
linux-master
arch/mips/txx9/rbtx4927/irq.c
/* * Toshiba rbtx4927 specific setup * * Author: MontaVista Software, Inc. * [email protected] * * Copyright 2001-2002 MontaVista Software Inc. * * Copyright (C) 1996, 97, 2001, 04 Ralf Baechle ([email protected]) * Copyright (C) 2000 RidgeRun, Inc. * Author: RidgeRun, Inc. * [email protected], [email protected], [email protected] * * Copyright 2001 MontaVista Software Inc. * Author: [email protected] or [email protected] * * Copyright 2002 MontaVista Software Inc. * Author: Michael Pruznick, [email protected] * * Copyright (C) 2000-2001 Toshiba Corporation * * Copyright (C) 2004 MontaVista Software Inc. * Author: Manish Lachwani, [email protected] * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <linux/init.h> #include <linux/kernel.h> #include <linux/types.h> #include <linux/ioport.h> #include <linux/platform_device.h> #include <linux/delay.h> #include <linux/gpio.h> #include <linux/leds.h> #include <asm/io.h> #include <asm/reboot.h> #include <asm/txx9pio.h> #include <asm/txx9/generic.h> #include <asm/txx9/pci.h> #include <asm/txx9/rbtx4927.h> #include <asm/txx9/tx4938.h> /* for TX4937 */ #ifdef CONFIG_PCI static void __init tx4927_pci_setup(void) { int extarb = !(__raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_PCIARB); struct pci_controller *c = &txx9_primary_pcic; register_pci_controller(c); if (__raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_PCI66) txx9_pci_option = (txx9_pci_option & ~TXX9_PCI_OPT_CLK_MASK) | TXX9_PCI_OPT_CLK_66; /* already configured */ /* Reset PCI Bus */ writeb(1, rbtx4927_pcireset_addr); /* Reset PCIC */ txx9_set64(&tx4927_ccfgptr->clkctr, TX4927_CLKCTR_PCIRST); if ((txx9_pci_option & TXX9_PCI_OPT_CLK_MASK) == TXX9_PCI_OPT_CLK_66) tx4927_pciclk66_setup(); mdelay(10); /* clear PCIC reset */ txx9_clear64(&tx4927_ccfgptr->clkctr, TX4927_CLKCTR_PCIRST); writeb(0, rbtx4927_pcireset_addr); iob(); tx4927_report_pciclk(); tx4927_pcic_setup(tx4927_pcicptr, c, extarb); if ((txx9_pci_option & TXX9_PCI_OPT_CLK_MASK) == TXX9_PCI_OPT_CLK_AUTO && txx9_pci66_check(c, 0, 0)) { /* Reset PCI Bus */ writeb(1, rbtx4927_pcireset_addr); /* Reset PCIC */ txx9_set64(&tx4927_ccfgptr->clkctr, TX4927_CLKCTR_PCIRST); tx4927_pciclk66_setup(); mdelay(10); /* clear PCIC reset */ txx9_clear64(&tx4927_ccfgptr->clkctr, TX4927_CLKCTR_PCIRST); writeb(0, rbtx4927_pcireset_addr); iob(); /* Reinitialize PCIC */ tx4927_report_pciclk(); tx4927_pcic_setup(tx4927_pcicptr, c, extarb); } tx4927_setup_pcierr_irq(); } static void __init tx4937_pci_setup(void) { int extarb = !(__raw_readq(&tx4938_ccfgptr->ccfg) & TX4938_CCFG_PCIARB); struct pci_controller *c = &txx9_primary_pcic; register_pci_controller(c); if (__raw_readq(&tx4938_ccfgptr->ccfg) & TX4938_CCFG_PCI66) txx9_pci_option = (txx9_pci_option & ~TXX9_PCI_OPT_CLK_MASK) | TXX9_PCI_OPT_CLK_66; /* already configured */ /* Reset PCI Bus */ writeb(1, rbtx4927_pcireset_addr); /* Reset PCIC */ txx9_set64(&tx4938_ccfgptr->clkctr, TX4938_CLKCTR_PCIRST); if ((txx9_pci_option & TXX9_PCI_OPT_CLK_MASK) == TXX9_PCI_OPT_CLK_66) tx4938_pciclk66_setup(); mdelay(10); /* clear PCIC reset */ txx9_clear64(&tx4938_ccfgptr->clkctr, TX4938_CLKCTR_PCIRST); writeb(0, rbtx4927_pcireset_addr); iob(); tx4938_report_pciclk(); tx4927_pcic_setup(tx4938_pcicptr, c, extarb); if ((txx9_pci_option & TXX9_PCI_OPT_CLK_MASK) == TXX9_PCI_OPT_CLK_AUTO && txx9_pci66_check(c, 0, 0)) { /* Reset PCI Bus */ writeb(1, rbtx4927_pcireset_addr); /* Reset PCIC */ txx9_set64(&tx4938_ccfgptr->clkctr, TX4938_CLKCTR_PCIRST); tx4938_pciclk66_setup(); mdelay(10); /* clear PCIC reset */ txx9_clear64(&tx4938_ccfgptr->clkctr, TX4938_CLKCTR_PCIRST); writeb(0, rbtx4927_pcireset_addr); iob(); /* Reinitialize PCIC */ tx4938_report_pciclk(); tx4927_pcic_setup(tx4938_pcicptr, c, extarb); } tx4938_setup_pcierr_irq(); } #else static inline void tx4927_pci_setup(void) {} static inline void tx4937_pci_setup(void) {} #endif /* CONFIG_PCI */ static void __init rbtx4927_gpio_init(void) { /* TX4927-SIO DTR on (PIO[15]) */ gpio_request(15, "sio-dtr"); gpio_direction_output(15, 1); tx4927_sio_init(0, 0); } static void __init rbtx4927_arch_init(void) { txx9_gpio_init(TX4927_PIO_REG & 0xfffffffffULL, 0, TX4927_NUM_PIO); rbtx4927_gpio_init(); tx4927_pci_setup(); } static void __init rbtx4937_arch_init(void) { txx9_gpio_init(TX4938_PIO_REG & 0xfffffffffULL, 0, TX4938_NUM_PIO); rbtx4927_gpio_init(); tx4937_pci_setup(); } static void toshiba_rbtx4927_restart(char *command) { /* enable the s/w reset register */ writeb(1, rbtx4927_softresetlock_addr); /* wait for enable to be seen */ while (!(readb(rbtx4927_softresetlock_addr) & 1)) ; /* do a s/w reset */ writeb(1, rbtx4927_softreset_addr); /* fallback */ (*_machine_halt)(); } static void __init rbtx4927_clock_init(void); static void __init rbtx4937_clock_init(void); static void __init rbtx4927_mem_setup(void) { if (TX4927_REV_PCODE() == 0x4927) { rbtx4927_clock_init(); tx4927_setup(); } else { rbtx4937_clock_init(); tx4938_setup(); } _machine_restart = toshiba_rbtx4927_restart; #ifdef CONFIG_PCI txx9_alloc_pci_controller(&txx9_primary_pcic, RBTX4927_PCIMEM, RBTX4927_PCIMEM_SIZE, RBTX4927_PCIIO, RBTX4927_PCIIO_SIZE); txx9_board_pcibios_setup = tx4927_pcibios_setup; #else set_io_port_base(KSEG1 + RBTX4927_ISA_IO_OFFSET); #endif } static void __init rbtx4927_clock_init(void) { /* * ASSUMPTION: PCIDIVMODE is configured for PCI 33MHz or 66MHz. * * For TX4927: * PCIDIVMODE[12:11]'s initial value is given by S9[4:3] (ON:0, OFF:1). * CPU 166MHz: PCI 66MHz : PCIDIVMODE: 00 (1/2.5) * CPU 200MHz: PCI 66MHz : PCIDIVMODE: 01 (1/3) * CPU 166MHz: PCI 33MHz : PCIDIVMODE: 10 (1/5) * CPU 200MHz: PCI 33MHz : PCIDIVMODE: 11 (1/6) * i.e. S9[3]: ON (83MHz), OFF (100MHz) */ switch ((unsigned long)__raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_PCIDIVMODE_MASK) { case TX4927_CCFG_PCIDIVMODE_2_5: case TX4927_CCFG_PCIDIVMODE_5: txx9_cpu_clock = 166666666; /* 166MHz */ break; default: txx9_cpu_clock = 200000000; /* 200MHz */ } } static void __init rbtx4937_clock_init(void) { /* * ASSUMPTION: PCIDIVMODE is configured for PCI 33MHz or 66MHz. * * For TX4937: * PCIDIVMODE[12:11]'s initial value is given by S1[5:4] (ON:0, OFF:1) * PCIDIVMODE[10] is 0. * CPU 266MHz: PCI 33MHz : PCIDIVMODE: 000 (1/8) * CPU 266MHz: PCI 66MHz : PCIDIVMODE: 001 (1/4) * CPU 300MHz: PCI 33MHz : PCIDIVMODE: 010 (1/9) * CPU 300MHz: PCI 66MHz : PCIDIVMODE: 011 (1/4.5) * CPU 333MHz: PCI 33MHz : PCIDIVMODE: 100 (1/10) * CPU 333MHz: PCI 66MHz : PCIDIVMODE: 101 (1/5) */ switch ((unsigned long)__raw_readq(&tx4938_ccfgptr->ccfg) & TX4938_CCFG_PCIDIVMODE_MASK) { case TX4938_CCFG_PCIDIVMODE_8: case TX4938_CCFG_PCIDIVMODE_4: txx9_cpu_clock = 266666666; /* 266MHz */ break; case TX4938_CCFG_PCIDIVMODE_9: case TX4938_CCFG_PCIDIVMODE_4_5: txx9_cpu_clock = 300000000; /* 300MHz */ break; default: txx9_cpu_clock = 333333333; /* 333MHz */ } } static void __init rbtx4927_time_init(void) { tx4927_time_init(0); } static void __init toshiba_rbtx4927_rtc_init(void) { struct resource res = { .start = RBTX4927_BRAMRTC_BASE - IO_BASE, .end = RBTX4927_BRAMRTC_BASE - IO_BASE + 0x800 - 1, .flags = IORESOURCE_MEM, }; platform_device_register_simple("rtc-ds1742", -1, &res, 1); } static void __init rbtx4927_ne_init(void) { struct resource res[] = { { .start = RBTX4927_RTL_8019_BASE, .end = RBTX4927_RTL_8019_BASE + 0x20 - 1, .flags = IORESOURCE_IO, }, { .start = RBTX4927_RTL_8019_IRQ, .flags = IORESOURCE_IRQ, } }; platform_device_register_simple("ne", -1, res, ARRAY_SIZE(res)); } static void __init rbtx4927_mtd_init(void) { int i; for (i = 0; i < 2; i++) tx4927_mtd_init(i); } static void __init rbtx4927_gpioled_init(void) { static const struct gpio_led leds[] = { { .name = "gpioled:green:0", .gpio = 0, .active_low = 1, }, { .name = "gpioled:green:1", .gpio = 1, .active_low = 1, }, }; static struct gpio_led_platform_data pdata = { .num_leds = ARRAY_SIZE(leds), .leds = leds, }; struct platform_device *pdev = platform_device_alloc("leds-gpio", 0); if (!pdev) return; pdev->dev.platform_data = &pdata; if (platform_device_add(pdev)) platform_device_put(pdev); } static void __init rbtx4927_device_init(void) { toshiba_rbtx4927_rtc_init(); rbtx4927_ne_init(); tx4927_wdt_init(); rbtx4927_mtd_init(); if (TX4927_REV_PCODE() == 0x4927) { tx4927_dmac_init(2); tx4927_aclc_init(0, 1); } else { tx4938_dmac_init(0, 2); tx4938_aclc_init(); } platform_device_register_simple("txx9aclc-generic", -1, NULL, 0); txx9_iocled_init(RBTX4927_LED_ADDR - IO_BASE, -1, 3, 1, "green", NULL); rbtx4927_gpioled_init(); } struct txx9_board_vec rbtx4927_vec __initdata = { .system = "Toshiba RBTX4927", .prom_init = rbtx4927_prom_init, .mem_setup = rbtx4927_mem_setup, .irq_setup = rbtx4927_irq_setup, .time_init = rbtx4927_time_init, .device_init = rbtx4927_device_init, .arch_init = rbtx4927_arch_init, #ifdef CONFIG_PCI .pci_map_irq = rbtx4927_pci_map_irq, #endif }; struct txx9_board_vec rbtx4937_vec __initdata = { .system = "Toshiba RBTX4937", .prom_init = rbtx4927_prom_init, .mem_setup = rbtx4927_mem_setup, .irq_setup = rbtx4927_irq_setup, .time_init = rbtx4927_time_init, .device_init = rbtx4927_device_init, .arch_init = rbtx4937_arch_init, #ifdef CONFIG_PCI .pci_map_irq = rbtx4927_pci_map_irq, #endif };
linux-master
arch/mips/txx9/rbtx4927/setup.c
/* * rbtx4927 specific prom routines * * Author: MontaVista Software, Inc. * [email protected] * * Copyright 2001-2002 MontaVista Software Inc. * * Copyright (C) 2004 MontaVista Software Inc. * Author: Manish Lachwani, [email protected] * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <linux/init.h> #include <linux/memblock.h> #include <asm/txx9/generic.h> #include <asm/txx9/rbtx4927.h> void __init rbtx4927_prom_init(void) { memblock_add(0, tx4927_get_mem_size()); txx9_sio_putchar_init(TX4927_SIO_REG(0) & 0xfffffffffULL); }
linux-master
arch/mips/txx9/rbtx4927/prom.c
/* * TX4938/4937 setup routines * Based on linux/arch/mips/txx9/rbtx4938/setup.c, * and RBTX49xx patch from CELF patch archive. * * 2003-2005 (c) MontaVista Software, Inc. * (C) Copyright TOSHIBA CORPORATION 2000-2001, 2004-2007 * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. */ #include <linux/init.h> #include <linux/ioport.h> #include <linux/delay.h> #include <linux/param.h> #include <linux/ptrace.h> #include <linux/mtd/physmap.h> #include <linux/platform_device.h> #include <linux/platform_data/txx9/ndfmc.h> #include <asm/reboot.h> #include <asm/traps.h> #include <asm/txx9irq.h> #include <asm/txx9tmr.h> #include <asm/txx9pio.h> #include <asm/txx9/generic.h> #include <asm/txx9/dmac.h> #include <asm/txx9/tx4938.h> static void __init tx4938_wdr_init(void) { /* report watchdog reset status */ if (____raw_readq(&tx4938_ccfgptr->ccfg) & TX4938_CCFG_WDRST) pr_warn("Watchdog reset detected at 0x%lx\n", read_c0_errorepc()); /* clear WatchDogReset (W1C) */ tx4938_ccfg_set(TX4938_CCFG_WDRST); /* do reset on watchdog */ tx4938_ccfg_set(TX4938_CCFG_WR); } void __init tx4938_wdt_init(void) { txx9_wdt_init(TX4938_TMR_REG(2) & 0xfffffffffULL); } static void tx4938_machine_restart(char *command) { local_irq_disable(); pr_emerg("Rebooting (with %s watchdog reset)...\n", (____raw_readq(&tx4938_ccfgptr->ccfg) & TX4938_CCFG_WDREXEN) ? "external" : "internal"); /* clear watchdog status */ tx4938_ccfg_set(TX4938_CCFG_WDRST); /* W1C */ txx9_wdt_now(TX4938_TMR_REG(2) & 0xfffffffffULL); while (!(____raw_readq(&tx4938_ccfgptr->ccfg) & TX4938_CCFG_WDRST)) ; mdelay(10); if (____raw_readq(&tx4938_ccfgptr->ccfg) & TX4938_CCFG_WDREXEN) { pr_emerg("Rebooting (with internal watchdog reset)...\n"); /* External WDRST failed. Do internal watchdog reset */ tx4938_ccfg_clear(TX4938_CCFG_WDREXEN); } /* fallback */ (*_machine_halt)(); } void show_registers(struct pt_regs *regs); static int tx4938_be_handler(struct pt_regs *regs, int is_fixup) { int data = regs->cp0_cause & 4; console_verbose(); pr_err("%cBE exception at %#lx\n", data ? 'D' : 'I', regs->cp0_epc); pr_err("ccfg:%llx, toea:%llx\n", (unsigned long long)____raw_readq(&tx4938_ccfgptr->ccfg), (unsigned long long)____raw_readq(&tx4938_ccfgptr->toea)); #ifdef CONFIG_PCI tx4927_report_pcic_status(); #endif show_registers(regs); panic("BusError!"); } static void __init tx4938_be_init(void) { mips_set_be_handler(tx4938_be_handler); } static struct resource tx4938_sdram_resource[4]; static struct resource tx4938_sram_resource; #define TX4938_SRAM_SIZE 0x800 void __init tx4938_setup(void) { int i; __u32 divmode; unsigned int cpuclk = 0; u64 ccfg; txx9_reg_res_init(TX4938_REV_PCODE(), TX4938_REG_BASE, TX4938_REG_SIZE); set_c0_config(TX49_CONF_CWFON); /* SDRAMC,EBUSC are configured by PROM */ for (i = 0; i < 8; i++) { if (!(TX4938_EBUSC_CR(i) & 0x8)) continue; /* disabled */ txx9_ce_res[i].start = (unsigned long)TX4938_EBUSC_BA(i); txx9_ce_res[i].end = txx9_ce_res[i].start + TX4938_EBUSC_SIZE(i) - 1; request_resource(&iomem_resource, &txx9_ce_res[i]); } /* clocks */ ccfg = ____raw_readq(&tx4938_ccfgptr->ccfg); if (txx9_master_clock) { /* calculate gbus_clock and cpu_clock from master_clock */ divmode = (__u32)ccfg & TX4938_CCFG_DIVMODE_MASK; switch (divmode) { case TX4938_CCFG_DIVMODE_8: case TX4938_CCFG_DIVMODE_10: case TX4938_CCFG_DIVMODE_12: case TX4938_CCFG_DIVMODE_16: case TX4938_CCFG_DIVMODE_18: txx9_gbus_clock = txx9_master_clock * 4; break; default: txx9_gbus_clock = txx9_master_clock; } switch (divmode) { case TX4938_CCFG_DIVMODE_2: case TX4938_CCFG_DIVMODE_8: cpuclk = txx9_gbus_clock * 2; break; case TX4938_CCFG_DIVMODE_2_5: case TX4938_CCFG_DIVMODE_10: cpuclk = txx9_gbus_clock * 5 / 2; break; case TX4938_CCFG_DIVMODE_3: case TX4938_CCFG_DIVMODE_12: cpuclk = txx9_gbus_clock * 3; break; case TX4938_CCFG_DIVMODE_4: case TX4938_CCFG_DIVMODE_16: cpuclk = txx9_gbus_clock * 4; break; case TX4938_CCFG_DIVMODE_4_5: case TX4938_CCFG_DIVMODE_18: cpuclk = txx9_gbus_clock * 9 / 2; break; } txx9_cpu_clock = cpuclk; } else { if (txx9_cpu_clock == 0) txx9_cpu_clock = 300000000; /* 300MHz */ /* calculate gbus_clock and master_clock from cpu_clock */ cpuclk = txx9_cpu_clock; divmode = (__u32)ccfg & TX4938_CCFG_DIVMODE_MASK; switch (divmode) { case TX4938_CCFG_DIVMODE_2: case TX4938_CCFG_DIVMODE_8: txx9_gbus_clock = cpuclk / 2; break; case TX4938_CCFG_DIVMODE_2_5: case TX4938_CCFG_DIVMODE_10: txx9_gbus_clock = cpuclk * 2 / 5; break; case TX4938_CCFG_DIVMODE_3: case TX4938_CCFG_DIVMODE_12: txx9_gbus_clock = cpuclk / 3; break; case TX4938_CCFG_DIVMODE_4: case TX4938_CCFG_DIVMODE_16: txx9_gbus_clock = cpuclk / 4; break; case TX4938_CCFG_DIVMODE_4_5: case TX4938_CCFG_DIVMODE_18: txx9_gbus_clock = cpuclk * 2 / 9; break; } switch (divmode) { case TX4938_CCFG_DIVMODE_8: case TX4938_CCFG_DIVMODE_10: case TX4938_CCFG_DIVMODE_12: case TX4938_CCFG_DIVMODE_16: case TX4938_CCFG_DIVMODE_18: txx9_master_clock = txx9_gbus_clock / 4; break; default: txx9_master_clock = txx9_gbus_clock; } } /* change default value to udelay/mdelay take reasonable time */ loops_per_jiffy = txx9_cpu_clock / HZ / 2; /* CCFG */ tx4938_wdr_init(); /* clear BusErrorOnWrite flag (W1C) */ tx4938_ccfg_set(TX4938_CCFG_BEOW); /* enable Timeout BusError */ if (txx9_ccfg_toeon) tx4938_ccfg_set(TX4938_CCFG_TOE); /* DMA selection */ txx9_clear64(&tx4938_ccfgptr->pcfg, TX4938_PCFG_DMASEL_ALL); /* Use external clock for external arbiter */ if (!(____raw_readq(&tx4938_ccfgptr->ccfg) & TX4938_CCFG_PCIARB)) txx9_clear64(&tx4938_ccfgptr->pcfg, TX4938_PCFG_PCICLKEN_ALL); pr_info("%s -- %dMHz(M%dMHz) CRIR:%08x CCFG:%llx PCFG:%llx\n", txx9_pcode_str, (cpuclk + 500000) / 1000000, (txx9_master_clock + 500000) / 1000000, (__u32)____raw_readq(&tx4938_ccfgptr->crir), ____raw_readq(&tx4938_ccfgptr->ccfg), ____raw_readq(&tx4938_ccfgptr->pcfg)); pr_info("%s SDRAMC --", txx9_pcode_str); for (i = 0; i < 4; i++) { __u64 cr = TX4938_SDRAMC_CR(i); unsigned long base, size; if (!((__u32)cr & 0x00000400)) continue; /* disabled */ base = (unsigned long)(cr >> 49) << 21; size = (((unsigned long)(cr >> 33) & 0x7fff) + 1) << 21; pr_cont(" CR%d:%016llx", i, cr); tx4938_sdram_resource[i].name = "SDRAM"; tx4938_sdram_resource[i].start = base; tx4938_sdram_resource[i].end = base + size - 1; tx4938_sdram_resource[i].flags = IORESOURCE_MEM; request_resource(&iomem_resource, &tx4938_sdram_resource[i]); } pr_cont(" TR:%09llx\n", ____raw_readq(&tx4938_sdramcptr->tr)); /* SRAM */ if (txx9_pcode == 0x4938 && ____raw_readq(&tx4938_sramcptr->cr) & 1) { unsigned int size = TX4938_SRAM_SIZE; tx4938_sram_resource.name = "SRAM"; tx4938_sram_resource.start = (____raw_readq(&tx4938_sramcptr->cr) >> (39-11)) & ~(size - 1); tx4938_sram_resource.end = tx4938_sram_resource.start + TX4938_SRAM_SIZE - 1; tx4938_sram_resource.flags = IORESOURCE_MEM; request_resource(&iomem_resource, &tx4938_sram_resource); } /* TMR */ /* disable all timers */ for (i = 0; i < TX4938_NR_TMR; i++) txx9_tmr_init(TX4938_TMR_REG(i) & 0xfffffffffULL); /* PIO */ __raw_writel(0, &tx4938_pioptr->maskcpu); __raw_writel(0, &tx4938_pioptr->maskext); if (txx9_pcode == 0x4938) { __u64 pcfg = ____raw_readq(&tx4938_ccfgptr->pcfg); /* set PCIC1 reset */ txx9_set64(&tx4938_ccfgptr->clkctr, TX4938_CLKCTR_PCIC1RST); if (pcfg & (TX4938_PCFG_ETH0_SEL | TX4938_PCFG_ETH1_SEL)) { mdelay(1); /* at least 128 cpu clock */ /* clear PCIC1 reset */ txx9_clear64(&tx4938_ccfgptr->clkctr, TX4938_CLKCTR_PCIC1RST); } else { pr_info("%s: stop PCIC1\n", txx9_pcode_str); /* stop PCIC1 */ txx9_set64(&tx4938_ccfgptr->clkctr, TX4938_CLKCTR_PCIC1CKD); } if (!(pcfg & TX4938_PCFG_ETH0_SEL)) { pr_info("%s: stop ETH0\n", txx9_pcode_str); txx9_set64(&tx4938_ccfgptr->clkctr, TX4938_CLKCTR_ETH0RST); txx9_set64(&tx4938_ccfgptr->clkctr, TX4938_CLKCTR_ETH0CKD); } if (!(pcfg & TX4938_PCFG_ETH1_SEL)) { pr_info("%s: stop ETH1\n", txx9_pcode_str); txx9_set64(&tx4938_ccfgptr->clkctr, TX4938_CLKCTR_ETH1RST); txx9_set64(&tx4938_ccfgptr->clkctr, TX4938_CLKCTR_ETH1CKD); } } _machine_restart = tx4938_machine_restart; board_be_init = tx4938_be_init; } void __init tx4938_time_init(unsigned int tmrnr) { if (____raw_readq(&tx4938_ccfgptr->ccfg) & TX4938_CCFG_TINTDIS) txx9_clockevent_init(TX4938_TMR_REG(tmrnr) & 0xfffffffffULL, TXX9_IRQ_BASE + TX4938_IR_TMR(tmrnr), TXX9_IMCLK); } void __init tx4938_sio_init(unsigned int sclk, unsigned int cts_mask) { int i; unsigned int ch_mask = 0; if (__raw_readq(&tx4938_ccfgptr->pcfg) & TX4938_PCFG_ETH0_SEL) ch_mask |= 1 << 1; /* disable SIO1 by PCFG setting */ for (i = 0; i < 2; i++) { if ((1 << i) & ch_mask) continue; txx9_sio_init(TX4938_SIO_REG(i) & 0xfffffffffULL, TXX9_IRQ_BASE + TX4938_IR_SIO(i), i, sclk, (1 << i) & cts_mask); } } void __init tx4938_spi_init(int busid) { txx9_spi_init(busid, TX4938_SPI_REG & 0xfffffffffULL, TXX9_IRQ_BASE + TX4938_IR_SPI); } void __init tx4938_ethaddr_init(unsigned char *addr0, unsigned char *addr1) { u64 pcfg = __raw_readq(&tx4938_ccfgptr->pcfg); if (addr0 && (pcfg & TX4938_PCFG_ETH0_SEL)) txx9_ethaddr_init(TXX9_IRQ_BASE + TX4938_IR_ETH0, addr0); if (addr1 && (pcfg & TX4938_PCFG_ETH1_SEL)) txx9_ethaddr_init(TXX9_IRQ_BASE + TX4938_IR_ETH1, addr1); } void __init tx4938_mtd_init(int ch) { struct physmap_flash_data pdata = { .width = TX4938_EBUSC_WIDTH(ch) / 8, }; unsigned long start = txx9_ce_res[ch].start; unsigned long size = txx9_ce_res[ch].end - start + 1; if (!(TX4938_EBUSC_CR(ch) & 0x8)) return; /* disabled */ txx9_physmap_flash_init(ch, start, size, &pdata); } void __init tx4938_ata_init(unsigned int irq, unsigned int shift, int tune) { struct platform_device *pdev; struct resource res[] = { { /* .start and .end are filled in later */ .flags = IORESOURCE_MEM, }, { .start = irq, .flags = IORESOURCE_IRQ, }, }; struct tx4938ide_platform_info pdata = { .ioport_shift = shift, /* * The IDE driver should not change bus timings if other ISA * devices existed. */ .gbus_clock = tune ? txx9_gbus_clock : 0, }; u64 ebccr; int i; if ((__raw_readq(&tx4938_ccfgptr->pcfg) & (TX4938_PCFG_ATA_SEL | TX4938_PCFG_NDF_SEL)) != TX4938_PCFG_ATA_SEL) return; for (i = 0; i < 8; i++) { /* check EBCCRn.ISA, EBCCRn.BSZ, EBCCRn.ME */ ebccr = __raw_readq(&tx4938_ebuscptr->cr[i]); if ((ebccr & 0x00f00008) == 0x00e00008) break; } if (i == 8) return; pdata.ebus_ch = i; res[0].start = ((ebccr >> 48) << 20) + 0x10000; res[0].end = res[0].start + 0x20000 - 1; pdev = platform_device_alloc("tx4938ide", -1); if (!pdev || platform_device_add_resources(pdev, res, ARRAY_SIZE(res)) || platform_device_add_data(pdev, &pdata, sizeof(pdata)) || platform_device_add(pdev)) platform_device_put(pdev); } void __init tx4938_ndfmc_init(unsigned int hold, unsigned int spw) { struct txx9ndfmc_platform_data plat_data = { .shift = 1, .gbus_clock = txx9_gbus_clock, .hold = hold, .spw = spw, .ch_mask = 1, }; unsigned long baseaddr = TX4938_NDFMC_REG & 0xfffffffffULL; #ifdef __BIG_ENDIAN baseaddr += 4; #endif if ((__raw_readq(&tx4938_ccfgptr->pcfg) & (TX4938_PCFG_ATA_SEL|TX4938_PCFG_ISA_SEL|TX4938_PCFG_NDF_SEL)) == TX4938_PCFG_NDF_SEL) txx9_ndfmc_init(baseaddr, &plat_data); } void __init tx4938_dmac_init(int memcpy_chan0, int memcpy_chan1) { struct txx9dmac_platform_data plat_data = { .have_64bit_regs = true, }; int i; for (i = 0; i < 2; i++) { plat_data.memcpy_chan = i ? memcpy_chan1 : memcpy_chan0; txx9_dmac_init(i, TX4938_DMA_REG(i) & 0xfffffffffULL, TXX9_IRQ_BASE + TX4938_IR_DMA(i, 0), &plat_data); } } void __init tx4938_aclc_init(void) { u64 pcfg = __raw_readq(&tx4938_ccfgptr->pcfg); if ((pcfg & TX4938_PCFG_SEL2) && !(pcfg & TX4938_PCFG_ETH0_SEL)) txx9_aclc_init(TX4938_ACLC_REG & 0xfffffffffULL, TXX9_IRQ_BASE + TX4938_IR_ACLC, 1, 0, 1); } void __init tx4938_sramc_init(void) { if (tx4938_sram_resource.start) txx9_sramc_init(&tx4938_sram_resource); } static void __init tx4938_stop_unused_modules(void) { __u64 pcfg, rst = 0, ckd = 0; char buf[128]; buf[0] = '\0'; local_irq_disable(); pcfg = ____raw_readq(&tx4938_ccfgptr->pcfg); switch (txx9_pcode) { case 0x4937: if (!(pcfg & TX4938_PCFG_SEL2)) { rst |= TX4938_CLKCTR_ACLRST; ckd |= TX4938_CLKCTR_ACLCKD; strcat(buf, " ACLC"); } break; case 0x4938: if (!(pcfg & TX4938_PCFG_SEL2) || (pcfg & TX4938_PCFG_ETH0_SEL)) { rst |= TX4938_CLKCTR_ACLRST; ckd |= TX4938_CLKCTR_ACLCKD; strcat(buf, " ACLC"); } if ((pcfg & (TX4938_PCFG_ATA_SEL | TX4938_PCFG_ISA_SEL | TX4938_PCFG_NDF_SEL)) != TX4938_PCFG_NDF_SEL) { rst |= TX4938_CLKCTR_NDFRST; ckd |= TX4938_CLKCTR_NDFCKD; strcat(buf, " NDFMC"); } if (!(pcfg & TX4938_PCFG_SPI_SEL)) { rst |= TX4938_CLKCTR_SPIRST; ckd |= TX4938_CLKCTR_SPICKD; strcat(buf, " SPI"); } break; } if (rst | ckd) { txx9_set64(&tx4938_ccfgptr->clkctr, rst); txx9_set64(&tx4938_ccfgptr->clkctr, ckd); } local_irq_enable(); if (buf[0]) pr_info("%s: stop%s\n", txx9_pcode_str, buf); } static int __init tx4938_late_init(void) { if (txx9_pcode != 0x4937 && txx9_pcode != 0x4938) return -ENODEV; tx4938_stop_unused_modules(); return 0; } late_initcall(tx4938_late_init);
linux-master
arch/mips/txx9/generic/setup_tx4938.c
/* * Common tx4927 irq handler * * Author: MontaVista Software, Inc. * [email protected] * * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <linux/init.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <asm/irq_cpu.h> #include <asm/txx9/tx4927.h> void __init tx4927_irq_init(void) { int i; mips_cpu_irq_init(); txx9_irq_init(TX4927_IRC_REG & 0xfffffffffULL); irq_set_chained_handler(MIPS_CPU_IRQ_BASE + TX4927_IRC_INT, handle_simple_irq); /* raise priority for errors, timers, SIO */ txx9_irq_set_pri(TX4927_IR_ECCERR, 7); txx9_irq_set_pri(TX4927_IR_WTOERR, 7); txx9_irq_set_pri(TX4927_IR_PCIERR, 7); txx9_irq_set_pri(TX4927_IR_PCIPME, 7); for (i = 0; i < TX4927_NUM_IR_TMR; i++) txx9_irq_set_pri(TX4927_IR_TMR(i), 6); for (i = 0; i < TX4927_NUM_IR_SIO; i++) txx9_irq_set_pri(TX4927_IR_SIO(i), 5); }
linux-master
arch/mips/txx9/generic/irq_tx4927.c
/* * linux/arch/mips/txx9/pci.c * * Based on linux/arch/mips/txx9/rbtx4927/setup.c, * linux/arch/mips/txx9/rbtx4938/setup.c, * and RBTX49xx patch from CELF patch archive. * * Copyright 2001-2005 MontaVista Software Inc. * Copyright (C) 1996, 97, 2001, 04 Ralf Baechle ([email protected]) * (C) Copyright TOSHIBA CORPORATION 2000-2001, 2004-2007 * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. */ #include <linux/delay.h> #include <linux/jiffies.h> #include <linux/io.h> #include <asm/txx9/generic.h> #include <asm/txx9/pci.h> #ifdef CONFIG_TOSHIBA_FPCIB0 #include <linux/interrupt.h> #include <linux/slab.h> #include <asm/i8259.h> #include <asm/txx9/smsc_fdc37m81x.h> #endif static int __init early_read_config_word(struct pci_controller *hose, int top_bus, int bus, int devfn, int offset, u16 *value) { struct pci_bus fake_bus; fake_bus.number = bus; fake_bus.sysdata = hose; fake_bus.ops = hose->pci_ops; if (bus != top_bus) /* Fake a parent bus structure. */ fake_bus.parent = &fake_bus; else fake_bus.parent = NULL; return pci_bus_read_config_word(&fake_bus, devfn, offset, value); } int __init txx9_pci66_check(struct pci_controller *hose, int top_bus, int current_bus) { u32 pci_devfn; unsigned short vid; int cap66 = -1; u16 stat; int ret; /* It seems SLC90E66 needs some time after PCI reset... */ mdelay(80); pr_info("PCI: Checking 66MHz capabilities...\n"); for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) { if (PCI_FUNC(pci_devfn)) continue; ret = early_read_config_word(hose, top_bus, current_bus, pci_devfn, PCI_VENDOR_ID, &vid); if (ret != PCIBIOS_SUCCESSFUL) continue; if (vid == 0xffff) continue; /* check 66MHz capability */ if (cap66 < 0) cap66 = 1; if (cap66) { early_read_config_word(hose, top_bus, current_bus, pci_devfn, PCI_STATUS, &stat); if (!(stat & PCI_STATUS_66MHZ)) { pr_debug("PCI: %02x:%02x not 66MHz capable.\n", current_bus, pci_devfn); cap66 = 0; break; } } } return cap66 > 0; } static struct resource primary_pci_mem_res[2] = { { .name = "PCI MEM" }, { .name = "PCI MMIO" }, }; static struct resource primary_pci_io_res = { .name = "PCI IO" }; struct pci_controller txx9_primary_pcic = { .mem_resource = &primary_pci_mem_res[0], .io_resource = &primary_pci_io_res, }; #ifdef CONFIG_64BIT int txx9_pci_mem_high __initdata = 1; #else int txx9_pci_mem_high __initdata; #endif /* * allocate pci_controller and resources. * mem_base, io_base: physical address. 0 for auto assignment. * mem_size and io_size means max size on auto assignment. * pcic must be &txx9_primary_pcic or NULL. */ struct pci_controller *__init txx9_alloc_pci_controller(struct pci_controller *pcic, unsigned long mem_base, unsigned long mem_size, unsigned long io_base, unsigned long io_size) { struct pcic { struct pci_controller c; struct resource r_mem[2]; struct resource r_io; } *new = NULL; int min_size = 0x10000; if (!pcic) { new = kzalloc(sizeof(*new), GFP_KERNEL); if (!new) return NULL; new->r_mem[0].name = "PCI mem"; new->r_mem[1].name = "PCI mmio"; new->r_io.name = "PCI io"; new->c.mem_resource = new->r_mem; new->c.io_resource = &new->r_io; pcic = &new->c; } else BUG_ON(pcic != &txx9_primary_pcic); pcic->io_resource->flags = IORESOURCE_IO; /* * for auto assignment, first search a (big) region for PCI * MEM, then search a region for PCI IO. */ if (mem_base) { pcic->mem_resource[0].start = mem_base; pcic->mem_resource[0].end = mem_base + mem_size - 1; if (request_resource(&iomem_resource, &pcic->mem_resource[0])) goto free_and_exit; } else { unsigned long min = 0, max = 0x20000000; /* low 512MB */ if (!mem_size) { /* default size for auto assignment */ if (txx9_pci_mem_high) mem_size = 0x20000000; /* mem:512M(max) */ else mem_size = 0x08000000; /* mem:128M(max) */ } if (txx9_pci_mem_high) { min = 0x20000000; max = 0xe0000000; } /* search free region for PCI MEM */ for (; mem_size >= min_size; mem_size /= 2) { if (allocate_resource(&iomem_resource, &pcic->mem_resource[0], mem_size, min, max, mem_size, NULL, NULL) == 0) break; } if (mem_size < min_size) goto free_and_exit; } pcic->mem_resource[1].flags = IORESOURCE_MEM | IORESOURCE_BUSY; if (io_base) { pcic->mem_resource[1].start = io_base; pcic->mem_resource[1].end = io_base + io_size - 1; if (request_resource(&iomem_resource, &pcic->mem_resource[1])) goto release_and_exit; } else { if (!io_size) /* default size for auto assignment */ io_size = 0x01000000; /* io:16M(max) */ /* search free region for PCI IO in low 512MB */ for (; io_size >= min_size; io_size /= 2) { if (allocate_resource(&iomem_resource, &pcic->mem_resource[1], io_size, 0, 0x20000000, io_size, NULL, NULL) == 0) break; } if (io_size < min_size) goto release_and_exit; io_base = pcic->mem_resource[1].start; } pcic->mem_resource[0].flags = IORESOURCE_MEM; if (pcic == &txx9_primary_pcic && mips_io_port_base == (unsigned long)-1) { /* map ioport 0 to PCI I/O space address 0 */ set_io_port_base(IO_BASE + pcic->mem_resource[1].start); pcic->io_resource->start = 0; pcic->io_offset = 0; /* busaddr == ioaddr */ pcic->io_map_base = IO_BASE + pcic->mem_resource[1].start; } else { /* physaddr to ioaddr */ pcic->io_resource->start = io_base - (mips_io_port_base - IO_BASE); pcic->io_offset = io_base - (mips_io_port_base - IO_BASE); pcic->io_map_base = mips_io_port_base; } pcic->io_resource->end = pcic->io_resource->start + io_size - 1; pcic->mem_offset = 0; /* busaddr == physaddr */ pr_info("PCI: IO %pR MEM %pR\n", &pcic->mem_resource[1], &pcic->mem_resource[0]); /* register_pci_controller() will request MEM resource */ release_resource(&pcic->mem_resource[0]); return pcic; release_and_exit: release_resource(&pcic->mem_resource[0]); free_and_exit: kfree(new); pr_err("PCI: Failed to allocate resources.\n"); return NULL; } static int __init txx9_arch_pci_init(void) { PCIBIOS_MIN_IO = 0x8000; /* reserve legacy I/O space */ return 0; } arch_initcall(txx9_arch_pci_init); /* IRQ/IDSEL mapping */ int txx9_pci_option = #ifdef CONFIG_PICMG_PCI_BACKPLANE_DEFAULT TXX9_PCI_OPT_PICMG | #endif TXX9_PCI_OPT_CLK_AUTO; enum txx9_pci_err_action txx9_pci_err_action = TXX9_PCI_ERR_REPORT; #ifdef CONFIG_TOSHIBA_FPCIB0 static irqreturn_t i8259_interrupt(int irq, void *dev_id) { int isairq; isairq = i8259_irq(); if (unlikely(isairq <= I8259A_IRQ_BASE)) return IRQ_NONE; generic_handle_irq(isairq); return IRQ_HANDLED; } static int txx9_i8259_irq_setup(int irq) { int err; init_i8259_irqs(); err = request_irq(irq, &i8259_interrupt, IRQF_SHARED, "cascade(i8259)", (void *)(long)irq); if (!err) pr_info("PCI-ISA bridge PIC (irq %d)\n", irq); return err; } static void __ref quirk_slc90e66_bridge(struct pci_dev *dev) { int irq; /* PCI/ISA Bridge interrupt */ u8 reg_64; u32 reg_b0; u8 reg_e1; irq = pcibios_map_irq(dev, PCI_SLOT(dev->devfn), 1); /* INTA */ if (!irq) return; txx9_i8259_irq_setup(irq); pci_read_config_byte(dev, 0x64, &reg_64); pci_read_config_dword(dev, 0xb0, &reg_b0); pci_read_config_byte(dev, 0xe1, &reg_e1); /* serial irq control */ reg_64 = 0xd0; /* serial irq pin */ reg_b0 |= 0x00010000; /* ide irq on isa14 */ reg_e1 &= 0xf0; reg_e1 |= 0x0d; pci_write_config_byte(dev, 0x64, reg_64); pci_write_config_dword(dev, 0xb0, reg_b0); pci_write_config_byte(dev, 0xe1, reg_e1); smsc_fdc37m81x_init(0x3f0); smsc_fdc37m81x_config_beg(); smsc_fdc37m81x_config_set(SMSC_FDC37M81X_DNUM, SMSC_FDC37M81X_KBD); smsc_fdc37m81x_config_set(SMSC_FDC37M81X_INT, 1); smsc_fdc37m81x_config_set(SMSC_FDC37M81X_INT2, 12); smsc_fdc37m81x_config_set(SMSC_FDC37M81X_ACTIVE, 1); smsc_fdc37m81x_config_end(); } static void quirk_slc90e66_ide(struct pci_dev *dev) { unsigned char dat; int regs[2] = {0x41, 0x43}; int i; /* SMSC SLC90E66 IDE uses irq 14, 15 (default) */ pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 14); pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &dat); pr_info("PCI: %s: IRQ %02x", pci_name(dev), dat); /* enable SMSC SLC90E66 IDE */ for (i = 0; i < ARRAY_SIZE(regs); i++) { pci_read_config_byte(dev, regs[i], &dat); pci_write_config_byte(dev, regs[i], dat | 0x80); pci_read_config_byte(dev, regs[i], &dat); pr_cont(" IDETIM%d %02x", i, dat); } pci_read_config_byte(dev, 0x5c, &dat); /* * !!! DO NOT REMOVE THIS COMMENT IT IS REQUIRED BY SMSC !!! * * This line of code is intended to provide the user with a work * around solution to the anomalies cited in SMSC's anomaly sheet * entitled, "SLC90E66 Functional Rev.J_0.1 Anomalies"". * * !!! DO NOT REMOVE THIS COMMENT IT IS REQUIRED BY SMSC !!! */ dat |= 0x01; pci_write_config_byte(dev, 0x5c, dat); pci_read_config_byte(dev, 0x5c, &dat); pr_cont(" REG5C %02x\n", dat); } #endif /* CONFIG_TOSHIBA_FPCIB0 */ static void tc35815_fixup(struct pci_dev *dev) { /* This device may have PM registers but not they are not supported. */ if (dev->pm_cap) { dev_info(&dev->dev, "PM disabled\n"); dev->pm_cap = 0; } } static void final_fixup(struct pci_dev *dev) { unsigned long timeout; unsigned char bist; int ret; /* Do build-in self test */ ret = pci_read_config_byte(dev, PCI_BIST, &bist); if ((ret != PCIBIOS_SUCCESSFUL) || !(bist & PCI_BIST_CAPABLE)) return; pci_set_power_state(dev, PCI_D0); pr_info("PCI: %s BIST...", pci_name(dev)); pci_write_config_byte(dev, PCI_BIST, PCI_BIST_START); timeout = jiffies + HZ * 2; /* timeout after 2 sec */ do { pci_read_config_byte(dev, PCI_BIST, &bist); if (time_after(jiffies, timeout)) break; } while (bist & PCI_BIST_START); if (bist & (PCI_BIST_CODE_MASK | PCI_BIST_START)) pr_cont("failed. (0x%x)\n", bist); else pr_cont("OK.\n"); } #ifdef CONFIG_TOSHIBA_FPCIB0 #define PCI_DEVICE_ID_EFAR_SLC90E66_0 0x9460 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_0, quirk_slc90e66_bridge); DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_1, quirk_slc90e66_ide); DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_1, quirk_slc90e66_ide); #endif DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_NWU, tc35815_fixup); DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_TX4939, tc35815_fixup); DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, final_fixup); DECLARE_PCI_FIXUP_RESUME(PCI_ANY_ID, PCI_ANY_ID, final_fixup); int pcibios_plat_dev_init(struct pci_dev *dev) { return 0; } static int (*txx9_pci_map_irq)(const struct pci_dev *dev, u8 slot, u8 pin); int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin) { return txx9_pci_map_irq(dev, slot, pin); } char * (*txx9_board_pcibios_setup)(char *str) __initdata; char *__init txx9_pcibios_setup(char *str) { if (txx9_board_pcibios_setup && !txx9_board_pcibios_setup(str)) return NULL; if (!strcmp(str, "picmg")) { /* PICMG compliant backplane (TOSHIBA JMB-PICMG-ATX (5V or 3.3V), JMB-PICMG-L2 (5V only), etc.) */ txx9_pci_option |= TXX9_PCI_OPT_PICMG; return NULL; } else if (!strcmp(str, "nopicmg")) { /* non-PICMG compliant backplane (TOSHIBA RBHBK4100,RBHBK4200, Interface PCM-PCM05, etc.) */ txx9_pci_option &= ~TXX9_PCI_OPT_PICMG; return NULL; } else if (!strncmp(str, "clk=", 4)) { char *val = str + 4; txx9_pci_option &= ~TXX9_PCI_OPT_CLK_MASK; if (strcmp(val, "33") == 0) txx9_pci_option |= TXX9_PCI_OPT_CLK_33; else if (strcmp(val, "66") == 0) txx9_pci_option |= TXX9_PCI_OPT_CLK_66; else /* "auto" */ txx9_pci_option |= TXX9_PCI_OPT_CLK_AUTO; return NULL; } else if (!strncmp(str, "err=", 4)) { if (!strcmp(str + 4, "panic")) txx9_pci_err_action = TXX9_PCI_ERR_PANIC; else if (!strcmp(str + 4, "ignore")) txx9_pci_err_action = TXX9_PCI_ERR_IGNORE; return NULL; } txx9_pci_map_irq = txx9_board_vec->pci_map_irq; return str; }
linux-master
arch/mips/txx9/generic/pci.c
/* * Based on linux/arch/mips/txx9/rbtx4938/setup.c, * and RBTX49xx patch from CELF patch archive. * * 2003-2005 (c) MontaVista Software, Inc. * (C) Copyright TOSHIBA CORPORATION 2000-2001, 2004-2007 * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. */ #include <linux/init.h> #include <linux/kernel.h> #include <linux/types.h> #include <linux/interrupt.h> #include <linux/string.h> #include <linux/export.h> #include <linux/clk-provider.h> #include <linux/clkdev.h> #include <linux/err.h> #include <linux/gpio/driver.h> #include <linux/platform_device.h> #include <linux/platform_data/txx9/ndfmc.h> #include <linux/serial_core.h> #include <linux/mtd/physmap.h> #include <linux/leds.h> #include <linux/device.h> #include <linux/slab.h> #include <linux/io.h> #include <linux/irq.h> #include <asm/bootinfo.h> #include <asm/idle.h> #include <asm/time.h> #include <asm/reboot.h> #include <asm/r4kcache.h> #include <asm/setup.h> #include <asm/txx9/generic.h> #include <asm/txx9/pci.h> #include <asm/txx9tmr.h> #include <asm/txx9/dmac.h> #ifdef CONFIG_CPU_TX49XX #include <asm/txx9/tx4938.h> #endif /* EBUSC settings of TX4927, etc. */ struct resource txx9_ce_res[8]; static char txx9_ce_res_name[8][4]; /* "CEn" */ /* pcode, internal register */ unsigned int txx9_pcode; char txx9_pcode_str[8]; static struct resource txx9_reg_res = { .name = txx9_pcode_str, .flags = IORESOURCE_MEM, }; void __init txx9_reg_res_init(unsigned int pcode, unsigned long base, unsigned long size) { int i; for (i = 0; i < ARRAY_SIZE(txx9_ce_res); i++) { sprintf(txx9_ce_res_name[i], "CE%d", i); txx9_ce_res[i].flags = IORESOURCE_MEM; txx9_ce_res[i].name = txx9_ce_res_name[i]; } txx9_pcode = pcode; sprintf(txx9_pcode_str, "TX%x", pcode); if (base) { txx9_reg_res.start = base & 0xfffffffffULL; txx9_reg_res.end = (base & 0xfffffffffULL) + (size - 1); request_resource(&iomem_resource, &txx9_reg_res); } } /* clocks */ unsigned int txx9_master_clock; unsigned int txx9_cpu_clock; unsigned int txx9_gbus_clock; int txx9_ccfg_toeon __initdata = 1; #define BOARD_VEC(board) extern struct txx9_board_vec board; #include <asm/txx9/boards.h> #undef BOARD_VEC struct txx9_board_vec *txx9_board_vec __initdata; static char txx9_system_type[32]; static struct txx9_board_vec *board_vecs[] __initdata = { #define BOARD_VEC(board) &board, #include <asm/txx9/boards.h> #undef BOARD_VEC }; static struct txx9_board_vec *__init find_board_byname(const char *name) { int i; /* search board_vecs table */ for (i = 0; i < ARRAY_SIZE(board_vecs); i++) { if (strstr(board_vecs[i]->system, name)) return board_vecs[i]; } return NULL; } static void __init prom_init_cmdline(void) { int argc; int *argv32; int i; /* Always ignore the "-c" at argv[0] */ if (fw_arg0 >= CKSEG0 || fw_arg1 < CKSEG0) { /* * argc is not a valid number, or argv32 is not a valid * pointer */ argc = 0; argv32 = NULL; } else { argc = (int)fw_arg0; argv32 = (int *)fw_arg1; } arcs_cmdline[0] = '\0'; for (i = 1; i < argc; i++) { char *str = (char *)(long)argv32[i]; if (i != 1) strcat(arcs_cmdline, " "); if (strchr(str, ' ')) { strcat(arcs_cmdline, "\""); strcat(arcs_cmdline, str); strcat(arcs_cmdline, "\""); } else strcat(arcs_cmdline, str); } } static int txx9_ic_disable __initdata; static int txx9_dc_disable __initdata; #if defined(CONFIG_CPU_TX49XX) /* flush all cache on very early stage (before 4k_cache_init) */ static void __init early_flush_dcache(void) { unsigned int conf = read_c0_config(); unsigned int dc_size = 1 << (12 + ((conf & CONF_DC) >> 6)); unsigned int linesz = 32; unsigned long addr, end; end = INDEX_BASE + dc_size / 4; /* 4way, waybit=0 */ for (addr = INDEX_BASE; addr < end; addr += linesz) { cache_op(Index_Writeback_Inv_D, addr | 0); cache_op(Index_Writeback_Inv_D, addr | 1); cache_op(Index_Writeback_Inv_D, addr | 2); cache_op(Index_Writeback_Inv_D, addr | 3); } } static void __init txx9_cache_fixup(void) { unsigned int conf; conf = read_c0_config(); /* flush and disable */ if (txx9_ic_disable) { conf |= TX49_CONF_IC; write_c0_config(conf); } if (txx9_dc_disable) { early_flush_dcache(); conf |= TX49_CONF_DC; write_c0_config(conf); } /* enable cache */ conf = read_c0_config(); if (!txx9_ic_disable) conf &= ~TX49_CONF_IC; if (!txx9_dc_disable) conf &= ~TX49_CONF_DC; write_c0_config(conf); if (conf & TX49_CONF_IC) pr_info("TX49XX I-Cache disabled.\n"); if (conf & TX49_CONF_DC) pr_info("TX49XX D-Cache disabled.\n"); } #else static inline void txx9_cache_fixup(void) { } #endif static void __init preprocess_cmdline(void) { static char cmdline[COMMAND_LINE_SIZE] __initdata; char *s; strcpy(cmdline, arcs_cmdline); s = cmdline; arcs_cmdline[0] = '\0'; while (s && *s) { char *str = strsep(&s, " "); if (strncmp(str, "board=", 6) == 0) { txx9_board_vec = find_board_byname(str + 6); continue; } else if (strncmp(str, "masterclk=", 10) == 0) { unsigned int val; if (kstrtouint(str + 10, 10, &val) == 0) txx9_master_clock = val; continue; } else if (strcmp(str, "icdisable") == 0) { txx9_ic_disable = 1; continue; } else if (strcmp(str, "dcdisable") == 0) { txx9_dc_disable = 1; continue; } else if (strcmp(str, "toeoff") == 0) { txx9_ccfg_toeon = 0; continue; } else if (strcmp(str, "toeon") == 0) { txx9_ccfg_toeon = 1; continue; } if (arcs_cmdline[0]) strcat(arcs_cmdline, " "); strcat(arcs_cmdline, str); } txx9_cache_fixup(); } static void __init select_board(void) { const char *envstr; /* first, determine by "board=" argument in preprocess_cmdline() */ if (txx9_board_vec) return; /* next, determine by "board" envvar */ envstr = prom_getenv("board"); if (envstr) { txx9_board_vec = find_board_byname(envstr); if (txx9_board_vec) return; } /* select "default" board */ #ifdef CONFIG_CPU_TX49XX switch (TX4938_REV_PCODE()) { #ifdef CONFIG_TOSHIBA_RBTX4927 case 0x4927: txx9_board_vec = &rbtx4927_vec; break; case 0x4937: txx9_board_vec = &rbtx4937_vec; break; #endif } #endif } void __init prom_init(void) { prom_init_cmdline(); preprocess_cmdline(); select_board(); strcpy(txx9_system_type, txx9_board_vec->system); txx9_board_vec->prom_init(); } const char *get_system_type(void) { return txx9_system_type; } const char *__init prom_getenv(const char *name) { const s32 *str; if (fw_arg2 < CKSEG0) return NULL; str = (const s32 *)fw_arg2; /* YAMON style ("name", "value" pairs) */ while (str[0] && str[1]) { if (!strcmp((const char *)(unsigned long)str[0], name)) return (const char *)(unsigned long)str[1]; str += 2; } return NULL; } static void __noreturn txx9_machine_halt(void) { local_irq_disable(); clear_c0_status(ST0_IM); while (1) { if (cpu_wait) { (*cpu_wait)(); if (cpu_has_counter) { /* * Clear counter interrupt while it * breaks WAIT instruction even if * masked. */ write_c0_compare(0); } } } } /* Watchdog support */ void __init txx9_wdt_init(unsigned long base) { struct resource res = { .start = base, .end = base + 0x100 - 1, .flags = IORESOURCE_MEM, }; platform_device_register_simple("txx9wdt", -1, &res, 1); } void txx9_wdt_now(unsigned long base) { struct txx9_tmr_reg __iomem *tmrptr = ioremap(base, sizeof(struct txx9_tmr_reg)); /* disable watch dog timer */ __raw_writel(TXx9_TMWTMR_WDIS | TXx9_TMWTMR_TWC, &tmrptr->wtmr); __raw_writel(0, &tmrptr->tcr); /* kick watchdog */ __raw_writel(TXx9_TMWTMR_TWIE, &tmrptr->wtmr); __raw_writel(1, &tmrptr->cpra); /* immediate */ __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG, &tmrptr->tcr); } /* SPI support */ void __init txx9_spi_init(int busid, unsigned long base, int irq) { struct resource res[] = { { .start = base, .end = base + 0x20 - 1, .flags = IORESOURCE_MEM, }, { .start = irq, .flags = IORESOURCE_IRQ, }, }; platform_device_register_simple("spi_txx9", busid, res, ARRAY_SIZE(res)); } void __init txx9_ethaddr_init(unsigned int id, unsigned char *ethaddr) { struct platform_device *pdev = platform_device_alloc("tc35815-mac", id); if (!pdev || platform_device_add_data(pdev, ethaddr, 6) || platform_device_add(pdev)) platform_device_put(pdev); } void __init txx9_sio_init(unsigned long baseaddr, int irq, unsigned int line, unsigned int sclk, int nocts) { #ifdef CONFIG_SERIAL_TXX9 struct uart_port req; memset(&req, 0, sizeof(req)); req.line = line; req.iotype = UPIO_MEM; req.membase = ioremap(baseaddr, 0x24); req.mapbase = baseaddr; req.irq = irq; if (!nocts) req.flags |= UPF_BUGGY_UART /*HAVE_CTS_LINE*/; if (sclk) { req.flags |= UPF_MAGIC_MULTIPLIER /*USE_SCLK*/; req.uartclk = sclk; } else req.uartclk = TXX9_IMCLK; early_serial_txx9_setup(&req); #endif /* CONFIG_SERIAL_TXX9 */ } #ifdef CONFIG_EARLY_PRINTK static void null_prom_putchar(char c) { } void (*txx9_prom_putchar)(char c) = null_prom_putchar; void prom_putchar(char c) { txx9_prom_putchar(c); } static void __iomem *early_txx9_sio_port; static void early_txx9_sio_putchar(char c) { #define TXX9_SICISR 0x0c #define TXX9_SITFIFO 0x1c #define TXX9_SICISR_TXALS 0x00000002 while (!(__raw_readl(early_txx9_sio_port + TXX9_SICISR) & TXX9_SICISR_TXALS)) ; __raw_writel(c, early_txx9_sio_port + TXX9_SITFIFO); } void __init txx9_sio_putchar_init(unsigned long baseaddr) { early_txx9_sio_port = ioremap(baseaddr, 0x24); txx9_prom_putchar = early_txx9_sio_putchar; } #endif /* CONFIG_EARLY_PRINTK */ /* wrappers */ void __init plat_mem_setup(void) { ioport_resource.start = 0; ioport_resource.end = ~0UL; /* no limit */ iomem_resource.start = 0; iomem_resource.end = ~0UL; /* no limit */ /* fallback restart/halt routines */ _machine_restart = (void (*)(char *))txx9_machine_halt; _machine_halt = txx9_machine_halt; pm_power_off = txx9_machine_halt; #ifdef CONFIG_PCI pcibios_plat_setup = txx9_pcibios_setup; #endif txx9_board_vec->mem_setup(); } void __init arch_init_irq(void) { txx9_board_vec->irq_setup(); } void __init plat_time_init(void) { #ifdef CONFIG_CPU_TX49XX mips_hpt_frequency = txx9_cpu_clock / 2; #endif txx9_board_vec->time_init(); } static void txx9_clk_init(void) { struct clk_hw *hw; int error; hw = clk_hw_register_fixed_rate(NULL, "gbus", NULL, 0, txx9_gbus_clock); if (IS_ERR(hw)) { error = PTR_ERR(hw); goto fail; } hw = clk_hw_register_fixed_factor(NULL, "imbus", "gbus", 0, 1, 2); error = clk_hw_register_clkdev(hw, "imbus_clk", NULL); if (error) goto fail; #ifdef CONFIG_CPU_TX49XX if (TX4938_REV_PCODE() == 0x4938) { hw = clk_hw_register_fixed_factor(NULL, "spi", "gbus", 0, 1, 4); error = clk_hw_register_clkdev(hw, "spi-baseclk", NULL); if (error) goto fail; } #endif return; fail: pr_err("Failed to register clocks: %d\n", error); } static int __init _txx9_arch_init(void) { txx9_clk_init(); if (txx9_board_vec->arch_init) txx9_board_vec->arch_init(); return 0; } arch_initcall(_txx9_arch_init); static int __init _txx9_device_init(void) { if (txx9_board_vec->device_init) txx9_board_vec->device_init(); return 0; } device_initcall(_txx9_device_init); int (*txx9_irq_dispatch)(int pending); asmlinkage void plat_irq_dispatch(void) { int pending = read_c0_status() & read_c0_cause() & ST0_IM; int irq = txx9_irq_dispatch(pending); if (likely(irq >= 0)) do_IRQ(irq); else spurious_interrupt(); } /* see include/asm-mips/mach-tx39xx/mangle-port.h, for example. */ #ifdef NEEDS_TXX9_SWIZZLE_ADDR_B static unsigned long __swizzle_addr_none(unsigned long port) { return port; } unsigned long (*__swizzle_addr_b)(unsigned long port) = __swizzle_addr_none; EXPORT_SYMBOL(__swizzle_addr_b); #endif void __init txx9_physmap_flash_init(int no, unsigned long addr, unsigned long size, const struct physmap_flash_data *pdata) { #if IS_ENABLED(CONFIG_MTD_PHYSMAP) struct resource res = { .start = addr, .end = addr + size - 1, .flags = IORESOURCE_MEM, }; struct platform_device *pdev; static struct mtd_partition parts[2]; struct physmap_flash_data pdata_part; /* If this area contained boot area, make separate partition */ if (pdata->nr_parts == 0 && !pdata->parts && addr < 0x1fc00000 && addr + size > 0x1fc00000 && !parts[0].name) { parts[0].name = "boot"; parts[0].offset = 0x1fc00000 - addr; parts[0].size = addr + size - 0x1fc00000; parts[1].name = "user"; parts[1].offset = 0; parts[1].size = 0x1fc00000 - addr; pdata_part = *pdata; pdata_part.nr_parts = ARRAY_SIZE(parts); pdata_part.parts = parts; pdata = &pdata_part; } pdev = platform_device_alloc("physmap-flash", no); if (!pdev || platform_device_add_resources(pdev, &res, 1) || platform_device_add_data(pdev, pdata, sizeof(*pdata)) || platform_device_add(pdev)) platform_device_put(pdev); #endif } void __init txx9_ndfmc_init(unsigned long baseaddr, const struct txx9ndfmc_platform_data *pdata) { #if IS_ENABLED(CONFIG_MTD_NAND_TXX9NDFMC) struct resource res = { .start = baseaddr, .end = baseaddr + 0x1000 - 1, .flags = IORESOURCE_MEM, }; struct platform_device *pdev = platform_device_alloc("txx9ndfmc", -1); if (!pdev || platform_device_add_resources(pdev, &res, 1) || platform_device_add_data(pdev, pdata, sizeof(*pdata)) || platform_device_add(pdev)) platform_device_put(pdev); #endif } #if IS_ENABLED(CONFIG_LEDS_GPIO) static DEFINE_SPINLOCK(txx9_iocled_lock); #define TXX9_IOCLED_MAXLEDS 8 struct txx9_iocled_data { struct gpio_chip chip; u8 cur_val; void __iomem *mmioaddr; struct gpio_led_platform_data pdata; struct gpio_led leds[TXX9_IOCLED_MAXLEDS]; char names[TXX9_IOCLED_MAXLEDS][32]; }; static int txx9_iocled_get(struct gpio_chip *chip, unsigned int offset) { struct txx9_iocled_data *data = gpiochip_get_data(chip); return !!(data->cur_val & (1 << offset)); } static void txx9_iocled_set(struct gpio_chip *chip, unsigned int offset, int value) { struct txx9_iocled_data *data = gpiochip_get_data(chip); unsigned long flags; spin_lock_irqsave(&txx9_iocled_lock, flags); if (value) data->cur_val |= 1 << offset; else data->cur_val &= ~(1 << offset); writeb(data->cur_val, data->mmioaddr); mmiowb(); spin_unlock_irqrestore(&txx9_iocled_lock, flags); } static int txx9_iocled_dir_in(struct gpio_chip *chip, unsigned int offset) { return 0; } static int txx9_iocled_dir_out(struct gpio_chip *chip, unsigned int offset, int value) { txx9_iocled_set(chip, offset, value); return 0; } void __init txx9_iocled_init(unsigned long baseaddr, int basenum, unsigned int num, int lowactive, const char *color, char **deftriggers) { struct txx9_iocled_data *iocled; struct platform_device *pdev; int i; static char *default_triggers[] __initdata = { "heartbeat", "disk-activity", "nand-disk", NULL, }; if (!deftriggers) deftriggers = default_triggers; iocled = kzalloc(sizeof(*iocled), GFP_KERNEL); if (!iocled) return; iocled->mmioaddr = ioremap(baseaddr, 1); if (!iocled->mmioaddr) goto out_free; iocled->chip.get = txx9_iocled_get; iocled->chip.set = txx9_iocled_set; iocled->chip.direction_input = txx9_iocled_dir_in; iocled->chip.direction_output = txx9_iocled_dir_out; iocled->chip.label = "iocled"; iocled->chip.base = basenum; iocled->chip.ngpio = num; if (gpiochip_add_data(&iocled->chip, iocled)) goto out_unmap; if (basenum < 0) basenum = iocled->chip.base; pdev = platform_device_alloc("leds-gpio", basenum); if (!pdev) goto out_gpio; iocled->pdata.num_leds = num; iocled->pdata.leds = iocled->leds; for (i = 0; i < num; i++) { struct gpio_led *led = &iocled->leds[i]; snprintf(iocled->names[i], sizeof(iocled->names[i]), "iocled:%s:%u", color, i); led->name = iocled->names[i]; led->gpio = basenum + i; led->active_low = lowactive; if (deftriggers && *deftriggers) led->default_trigger = *deftriggers++; } pdev->dev.platform_data = &iocled->pdata; if (platform_device_add(pdev)) goto out_pdev; return; out_pdev: platform_device_put(pdev); out_gpio: gpiochip_remove(&iocled->chip); out_unmap: iounmap(iocled->mmioaddr); out_free: kfree(iocled); } #else /* CONFIG_LEDS_GPIO */ void __init txx9_iocled_init(unsigned long baseaddr, int basenum, unsigned int num, int lowactive, const char *color, char **deftriggers) { } #endif /* CONFIG_LEDS_GPIO */ void __init txx9_dmac_init(int id, unsigned long baseaddr, int irq, const struct txx9dmac_platform_data *pdata) { #if IS_ENABLED(CONFIG_TXX9_DMAC) struct resource res[] = { { .start = baseaddr, .end = baseaddr + 0x800 - 1, .flags = IORESOURCE_MEM, #ifndef CONFIG_MACH_TX49XX }, { .start = irq, .flags = IORESOURCE_IRQ, #endif } }; #ifdef CONFIG_MACH_TX49XX struct resource chan_res[] = { { .flags = IORESOURCE_IRQ, } }; #endif struct platform_device *pdev = platform_device_alloc("txx9dmac", id); struct txx9dmac_chan_platform_data cpdata; int i; if (!pdev || platform_device_add_resources(pdev, res, ARRAY_SIZE(res)) || platform_device_add_data(pdev, pdata, sizeof(*pdata)) || platform_device_add(pdev)) { platform_device_put(pdev); return; } memset(&cpdata, 0, sizeof(cpdata)); cpdata.dmac_dev = pdev; for (i = 0; i < TXX9_DMA_MAX_NR_CHANNELS; i++) { #ifdef CONFIG_MACH_TX49XX chan_res[0].start = irq + i; #endif pdev = platform_device_alloc("txx9dmac-chan", id * TXX9_DMA_MAX_NR_CHANNELS + i); if (!pdev || #ifdef CONFIG_MACH_TX49XX platform_device_add_resources(pdev, chan_res, ARRAY_SIZE(chan_res)) || #endif platform_device_add_data(pdev, &cpdata, sizeof(cpdata)) || platform_device_add(pdev)) platform_device_put(pdev); } #endif } void __init txx9_aclc_init(unsigned long baseaddr, int irq, unsigned int dmac_id, unsigned int dma_chan_out, unsigned int dma_chan_in) { } static struct bus_type txx9_sramc_subsys = { .name = "txx9_sram", .dev_name = "txx9_sram", }; struct txx9_sramc_dev { struct device dev; struct bin_attribute bindata_attr; void __iomem *base; }; static ssize_t txx9_sram_read(struct file *filp, struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t pos, size_t size) { struct txx9_sramc_dev *dev = bin_attr->private; size_t ramsize = bin_attr->size; if (pos >= ramsize) return 0; if (pos + size > ramsize) size = ramsize - pos; memcpy_fromio(buf, dev->base + pos, size); return size; } static ssize_t txx9_sram_write(struct file *filp, struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t pos, size_t size) { struct txx9_sramc_dev *dev = bin_attr->private; size_t ramsize = bin_attr->size; if (pos >= ramsize) return 0; if (pos + size > ramsize) size = ramsize - pos; memcpy_toio(dev->base + pos, buf, size); return size; } static void txx9_device_release(struct device *dev) { struct txx9_sramc_dev *tdev; tdev = container_of(dev, struct txx9_sramc_dev, dev); kfree(tdev); } void __init txx9_sramc_init(struct resource *r) { struct txx9_sramc_dev *dev; size_t size; int err; err = subsys_system_register(&txx9_sramc_subsys, NULL); if (err) return; dev = kzalloc(sizeof(*dev), GFP_KERNEL); if (!dev) return; size = resource_size(r); dev->base = ioremap(r->start, size); if (!dev->base) { kfree(dev); return; } dev->dev.release = &txx9_device_release; dev->dev.bus = &txx9_sramc_subsys; sysfs_bin_attr_init(&dev->bindata_attr); dev->bindata_attr.attr.name = "bindata"; dev->bindata_attr.attr.mode = S_IRUSR | S_IWUSR; dev->bindata_attr.read = txx9_sram_read; dev->bindata_attr.write = txx9_sram_write; dev->bindata_attr.size = size; dev->bindata_attr.private = dev; err = device_register(&dev->dev); if (err) goto exit_put; err = sysfs_create_bin_file(&dev->dev.kobj, &dev->bindata_attr); if (err) { iounmap(dev->base); device_unregister(&dev->dev); } return; exit_put: iounmap(dev->base); put_device(&dev->dev); }
linux-master
arch/mips/txx9/generic/setup.c
/* * common tx4927 memory interface * * Author: MontaVista Software, Inc. * [email protected] * * Copyright 2001-2002 MontaVista Software Inc. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <linux/init.h> #include <linux/types.h> #include <linux/io.h> #include <asm/txx9/tx4927.h> static unsigned int __init tx4927_process_sdccr(u64 __iomem *addr) { u64 val; unsigned int sdccr_ce; unsigned int sdccr_bs; unsigned int sdccr_rs; unsigned int sdccr_cs; unsigned int sdccr_mw; unsigned int bs = 0; unsigned int rs = 0; unsigned int cs = 0; unsigned int mw = 0; val = __raw_readq(addr); /* MVMCP -- need #defs for these bits masks */ sdccr_ce = ((val & (1 << 10)) >> 10); sdccr_bs = ((val & (1 << 8)) >> 8); sdccr_rs = ((val & (3 << 5)) >> 5); sdccr_cs = ((val & (7 << 2)) >> 2); sdccr_mw = ((val & (1 << 0)) >> 0); if (sdccr_ce) { bs = 2 << sdccr_bs; rs = 2048 << sdccr_rs; cs = 256 << sdccr_cs; mw = 8 >> sdccr_mw; } return rs * cs * mw * bs; } unsigned int __init tx4927_get_mem_size(void) { unsigned int total = 0; int i; for (i = 0; i < ARRAY_SIZE(tx4927_sdramcptr->cr); i++) total += tx4927_process_sdccr(&tx4927_sdramcptr->cr[i]); return total; }
linux-master
arch/mips/txx9/generic/mem_tx4927.c
/* * Interface for smsc fdc48m81x Super IO chip * * Author: MontaVista Software, Inc. [email protected] * * 2001-2003 (c) MontaVista Software, Inc. This file is licensed under * the terms of the GNU General Public License version 2. This program * is licensed "as is" without any warranty of any kind, whether express * or implied. * * Copyright 2004 (c) MontaVista Software, Inc. */ #include <linux/init.h> #include <linux/types.h> #include <asm/io.h> #include <asm/txx9/smsc_fdc37m81x.h> /* Common Registers */ #define SMSC_FDC37M81X_CONFIG_INDEX 0x00 #define SMSC_FDC37M81X_CONFIG_DATA 0x01 #define SMSC_FDC37M81X_CONF 0x02 #define SMSC_FDC37M81X_INDEX 0x03 #define SMSC_FDC37M81X_DNUM 0x07 #define SMSC_FDC37M81X_DID 0x20 #define SMSC_FDC37M81X_DREV 0x21 #define SMSC_FDC37M81X_PCNT 0x22 #define SMSC_FDC37M81X_PMGT 0x23 #define SMSC_FDC37M81X_OSC 0x24 #define SMSC_FDC37M81X_CONFPA0 0x26 #define SMSC_FDC37M81X_CONFPA1 0x27 #define SMSC_FDC37M81X_TEST4 0x2B #define SMSC_FDC37M81X_TEST5 0x2C #define SMSC_FDC37M81X_TEST1 0x2D #define SMSC_FDC37M81X_TEST2 0x2E #define SMSC_FDC37M81X_TEST3 0x2F /* Logical device numbers */ #define SMSC_FDC37M81X_FDD 0x00 #define SMSC_FDC37M81X_SERIAL1 0x04 #define SMSC_FDC37M81X_SERIAL2 0x05 #define SMSC_FDC37M81X_KBD 0x07 /* Logical device Config Registers */ #define SMSC_FDC37M81X_ACTIVE 0x30 #define SMSC_FDC37M81X_BASEADDR0 0x60 #define SMSC_FDC37M81X_BASEADDR1 0x61 #define SMSC_FDC37M81X_INT 0x70 #define SMSC_FDC37M81X_INT2 0x72 #define SMSC_FDC37M81X_MODE 0xF0 /* Chip Config Values */ #define SMSC_FDC37M81X_CONFIG_ENTER 0x55 #define SMSC_FDC37M81X_CONFIG_EXIT 0xaa #define SMSC_FDC37M81X_CHIP_ID 0x4d static unsigned long g_smsc_fdc37m81x_base; static inline unsigned char smsc_fdc37m81x_rd(unsigned char index) { outb(index, g_smsc_fdc37m81x_base + SMSC_FDC37M81X_CONFIG_INDEX); return inb(g_smsc_fdc37m81x_base + SMSC_FDC37M81X_CONFIG_DATA); } static inline void smsc_dc37m81x_wr(unsigned char index, unsigned char data) { outb(index, g_smsc_fdc37m81x_base + SMSC_FDC37M81X_CONFIG_INDEX); outb(data, g_smsc_fdc37m81x_base + SMSC_FDC37M81X_CONFIG_DATA); } void smsc_fdc37m81x_config_beg(void) { if (g_smsc_fdc37m81x_base) { outb(SMSC_FDC37M81X_CONFIG_ENTER, g_smsc_fdc37m81x_base + SMSC_FDC37M81X_CONFIG_INDEX); } } void smsc_fdc37m81x_config_end(void) { if (g_smsc_fdc37m81x_base) outb(SMSC_FDC37M81X_CONFIG_EXIT, g_smsc_fdc37m81x_base + SMSC_FDC37M81X_CONFIG_INDEX); } u8 smsc_fdc37m81x_config_get(u8 reg) { u8 val = 0; if (g_smsc_fdc37m81x_base) val = smsc_fdc37m81x_rd(reg); return val; } void smsc_fdc37m81x_config_set(u8 reg, u8 val) { if (g_smsc_fdc37m81x_base) smsc_dc37m81x_wr(reg, val); } unsigned long __init smsc_fdc37m81x_init(unsigned long port) { const int field = sizeof(unsigned long) * 2; u8 chip_id; if (g_smsc_fdc37m81x_base) pr_warn("%s: stepping on old base=0x%0*lx\n", __func__, field, g_smsc_fdc37m81x_base); g_smsc_fdc37m81x_base = port; smsc_fdc37m81x_config_beg(); chip_id = smsc_fdc37m81x_rd(SMSC_FDC37M81X_DID); if (chip_id == SMSC_FDC37M81X_CHIP_ID) smsc_fdc37m81x_config_end(); else { pr_warn("%s: unknown chip id 0x%02x\n", __func__, chip_id); g_smsc_fdc37m81x_base = 0; } return g_smsc_fdc37m81x_base; } #ifdef DEBUG static void smsc_fdc37m81x_config_dump_one(const char *key, u8 dev, u8 reg) { pr_info("%s: dev=0x%02x reg=0x%02x val=0x%02x\n", key, dev, reg, smsc_fdc37m81x_rd(reg)); } void smsc_fdc37m81x_config_dump(void) { u8 orig; const char *fname = __func__; smsc_fdc37m81x_config_beg(); orig = smsc_fdc37m81x_rd(SMSC_FDC37M81X_DNUM); pr_info("%s: common\n", fname); smsc_fdc37m81x_config_dump_one(fname, SMSC_FDC37M81X_NONE, SMSC_FDC37M81X_DNUM); smsc_fdc37m81x_config_dump_one(fname, SMSC_FDC37M81X_NONE, SMSC_FDC37M81X_DID); smsc_fdc37m81x_config_dump_one(fname, SMSC_FDC37M81X_NONE, SMSC_FDC37M81X_DREV); smsc_fdc37m81x_config_dump_one(fname, SMSC_FDC37M81X_NONE, SMSC_FDC37M81X_PCNT); smsc_fdc37m81x_config_dump_one(fname, SMSC_FDC37M81X_NONE, SMSC_FDC37M81X_PMGT); pr_info("%s: keyboard\n", fname); smsc_dc37m81x_wr(SMSC_FDC37M81X_DNUM, SMSC_FDC37M81X_KBD); smsc_fdc37m81x_config_dump_one(fname, SMSC_FDC37M81X_KBD, SMSC_FDC37M81X_ACTIVE); smsc_fdc37m81x_config_dump_one(fname, SMSC_FDC37M81X_KBD, SMSC_FDC37M81X_INT); smsc_fdc37m81x_config_dump_one(fname, SMSC_FDC37M81X_KBD, SMSC_FDC37M81X_INT2); smsc_fdc37m81x_config_dump_one(fname, SMSC_FDC37M81X_KBD, SMSC_FDC37M81X_LDCR_F0); smsc_dc37m81x_wr(SMSC_FDC37M81X_DNUM, orig); smsc_fdc37m81x_config_end(); } #endif
linux-master
arch/mips/txx9/generic/smsc_fdc37m81x.c
/* * linux/arch/mips/tx4938/common/irq.c * * Common tx4938 irq handler * Copyright (C) 2000-2001 Toshiba Corporation * * 2003-2005 (c) MontaVista Software, Inc. This file is licensed under the * terms of the GNU General Public License version 2. This program is * licensed "as is" without any warranty of any kind, whether express * or implied. * * Support for TX4938 in 2.6 - Manish Lachwani ([email protected]) */ #include <linux/init.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <asm/irq_cpu.h> #include <asm/txx9/tx4938.h> void __init tx4938_irq_init(void) { int i; mips_cpu_irq_init(); txx9_irq_init(TX4938_IRC_REG & 0xfffffffffULL); irq_set_chained_handler(MIPS_CPU_IRQ_BASE + TX4938_IRC_INT, handle_simple_irq); /* raise priority for errors, timers, SIO */ txx9_irq_set_pri(TX4938_IR_ECCERR, 7); txx9_irq_set_pri(TX4938_IR_WTOERR, 7); txx9_irq_set_pri(TX4938_IR_PCIERR, 7); txx9_irq_set_pri(TX4938_IR_PCIPME, 7); for (i = 0; i < TX4938_NUM_IR_TMR; i++) txx9_irq_set_pri(TX4938_IR_TMR(i), 6); for (i = 0; i < TX4938_NUM_IR_SIO; i++) txx9_irq_set_pri(TX4938_IR_SIO(i), 5); }
linux-master
arch/mips/txx9/generic/irq_tx4938.c
/* * TX4927 setup routines * Based on linux/arch/mips/txx9/rbtx4938/setup.c, * and RBTX49xx patch from CELF patch archive. * * 2003-2005 (c) MontaVista Software, Inc. * (C) Copyright TOSHIBA CORPORATION 2000-2001, 2004-2007 * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. */ #include <linux/init.h> #include <linux/ioport.h> #include <linux/delay.h> #include <linux/param.h> #include <linux/ptrace.h> #include <linux/mtd/physmap.h> #include <asm/reboot.h> #include <asm/traps.h> #include <asm/txx9irq.h> #include <asm/txx9tmr.h> #include <asm/txx9pio.h> #include <asm/txx9/generic.h> #include <asm/txx9/dmac.h> #include <asm/txx9/tx4927.h> static void __init tx4927_wdr_init(void) { /* report watchdog reset status */ if (____raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_WDRST) pr_warn("Watchdog reset detected at 0x%lx\n", read_c0_errorepc()); /* clear WatchDogReset (W1C) */ tx4927_ccfg_set(TX4927_CCFG_WDRST); /* do reset on watchdog */ tx4927_ccfg_set(TX4927_CCFG_WR); } void __init tx4927_wdt_init(void) { txx9_wdt_init(TX4927_TMR_REG(2) & 0xfffffffffULL); } static void tx4927_machine_restart(char *command) { local_irq_disable(); pr_emerg("Rebooting (with %s watchdog reset)...\n", (____raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_WDREXEN) ? "external" : "internal"); /* clear watchdog status */ tx4927_ccfg_set(TX4927_CCFG_WDRST); /* W1C */ txx9_wdt_now(TX4927_TMR_REG(2) & 0xfffffffffULL); while (!(____raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_WDRST)) ; mdelay(10); if (____raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_WDREXEN) { pr_emerg("Rebooting (with internal watchdog reset)...\n"); /* External WDRST failed. Do internal watchdog reset */ tx4927_ccfg_clear(TX4927_CCFG_WDREXEN); } /* fallback */ (*_machine_halt)(); } void show_registers(struct pt_regs *regs); static int tx4927_be_handler(struct pt_regs *regs, int is_fixup) { int data = regs->cp0_cause & 4; console_verbose(); pr_err("%cBE exception at %#lx\n", data ? 'D' : 'I', regs->cp0_epc); pr_err("ccfg:%llx, toea:%llx\n", (unsigned long long)____raw_readq(&tx4927_ccfgptr->ccfg), (unsigned long long)____raw_readq(&tx4927_ccfgptr->toea)); #ifdef CONFIG_PCI tx4927_report_pcic_status(); #endif show_registers(regs); panic("BusError!"); } static void __init tx4927_be_init(void) { mips_set_be_handler(tx4927_be_handler); } static struct resource tx4927_sdram_resource[4]; void __init tx4927_setup(void) { int i; __u32 divmode; unsigned int cpuclk = 0; u64 ccfg; txx9_reg_res_init(TX4927_REV_PCODE(), TX4927_REG_BASE, TX4927_REG_SIZE); set_c0_config(TX49_CONF_CWFON); /* SDRAMC,EBUSC are configured by PROM */ for (i = 0; i < 8; i++) { if (!(TX4927_EBUSC_CR(i) & 0x8)) continue; /* disabled */ txx9_ce_res[i].start = (unsigned long)TX4927_EBUSC_BA(i); txx9_ce_res[i].end = txx9_ce_res[i].start + TX4927_EBUSC_SIZE(i) - 1; request_resource(&iomem_resource, &txx9_ce_res[i]); } /* clocks */ ccfg = ____raw_readq(&tx4927_ccfgptr->ccfg); if (txx9_master_clock) { /* calculate gbus_clock and cpu_clock from master_clock */ divmode = (__u32)ccfg & TX4927_CCFG_DIVMODE_MASK; switch (divmode) { case TX4927_CCFG_DIVMODE_8: case TX4927_CCFG_DIVMODE_10: case TX4927_CCFG_DIVMODE_12: case TX4927_CCFG_DIVMODE_16: txx9_gbus_clock = txx9_master_clock * 4; break; default: txx9_gbus_clock = txx9_master_clock; } switch (divmode) { case TX4927_CCFG_DIVMODE_2: case TX4927_CCFG_DIVMODE_8: cpuclk = txx9_gbus_clock * 2; break; case TX4927_CCFG_DIVMODE_2_5: case TX4927_CCFG_DIVMODE_10: cpuclk = txx9_gbus_clock * 5 / 2; break; case TX4927_CCFG_DIVMODE_3: case TX4927_CCFG_DIVMODE_12: cpuclk = txx9_gbus_clock * 3; break; case TX4927_CCFG_DIVMODE_4: case TX4927_CCFG_DIVMODE_16: cpuclk = txx9_gbus_clock * 4; break; } txx9_cpu_clock = cpuclk; } else { if (txx9_cpu_clock == 0) txx9_cpu_clock = 200000000; /* 200MHz */ /* calculate gbus_clock and master_clock from cpu_clock */ cpuclk = txx9_cpu_clock; divmode = (__u32)ccfg & TX4927_CCFG_DIVMODE_MASK; switch (divmode) { case TX4927_CCFG_DIVMODE_2: case TX4927_CCFG_DIVMODE_8: txx9_gbus_clock = cpuclk / 2; break; case TX4927_CCFG_DIVMODE_2_5: case TX4927_CCFG_DIVMODE_10: txx9_gbus_clock = cpuclk * 2 / 5; break; case TX4927_CCFG_DIVMODE_3: case TX4927_CCFG_DIVMODE_12: txx9_gbus_clock = cpuclk / 3; break; case TX4927_CCFG_DIVMODE_4: case TX4927_CCFG_DIVMODE_16: txx9_gbus_clock = cpuclk / 4; break; } switch (divmode) { case TX4927_CCFG_DIVMODE_8: case TX4927_CCFG_DIVMODE_10: case TX4927_CCFG_DIVMODE_12: case TX4927_CCFG_DIVMODE_16: txx9_master_clock = txx9_gbus_clock / 4; break; default: txx9_master_clock = txx9_gbus_clock; } } /* change default value to udelay/mdelay take reasonable time */ loops_per_jiffy = txx9_cpu_clock / HZ / 2; /* CCFG */ tx4927_wdr_init(); /* clear BusErrorOnWrite flag (W1C) */ tx4927_ccfg_set(TX4927_CCFG_BEOW); /* enable Timeout BusError */ if (txx9_ccfg_toeon) tx4927_ccfg_set(TX4927_CCFG_TOE); /* DMA selection */ txx9_clear64(&tx4927_ccfgptr->pcfg, TX4927_PCFG_DMASEL_ALL); /* Use external clock for external arbiter */ if (!(____raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_PCIARB)) txx9_clear64(&tx4927_ccfgptr->pcfg, TX4927_PCFG_PCICLKEN_ALL); pr_info("%s -- %dMHz(M%dMHz) CRIR:%08x CCFG:%llx PCFG:%llx\n", txx9_pcode_str, (cpuclk + 500000) / 1000000, (txx9_master_clock + 500000) / 1000000, (__u32)____raw_readq(&tx4927_ccfgptr->crir), ____raw_readq(&tx4927_ccfgptr->ccfg), ____raw_readq(&tx4927_ccfgptr->pcfg)); pr_info("%s SDRAMC --", txx9_pcode_str); for (i = 0; i < 4; i++) { __u64 cr = TX4927_SDRAMC_CR(i); unsigned long base, size; if (!((__u32)cr & 0x00000400)) continue; /* disabled */ base = (unsigned long)(cr >> 49) << 21; size = (((unsigned long)(cr >> 33) & 0x7fff) + 1) << 21; pr_cont(" CR%d:%016llx", i, cr); tx4927_sdram_resource[i].name = "SDRAM"; tx4927_sdram_resource[i].start = base; tx4927_sdram_resource[i].end = base + size - 1; tx4927_sdram_resource[i].flags = IORESOURCE_MEM; request_resource(&iomem_resource, &tx4927_sdram_resource[i]); } pr_cont(" TR:%09llx\n", ____raw_readq(&tx4927_sdramcptr->tr)); /* TMR */ /* disable all timers */ for (i = 0; i < TX4927_NR_TMR; i++) txx9_tmr_init(TX4927_TMR_REG(i) & 0xfffffffffULL); /* PIO */ __raw_writel(0, &tx4927_pioptr->maskcpu); __raw_writel(0, &tx4927_pioptr->maskext); _machine_restart = tx4927_machine_restart; board_be_init = tx4927_be_init; } void __init tx4927_time_init(unsigned int tmrnr) { if (____raw_readq(&tx4927_ccfgptr->ccfg) & TX4927_CCFG_TINTDIS) txx9_clockevent_init(TX4927_TMR_REG(tmrnr) & 0xfffffffffULL, TXX9_IRQ_BASE + TX4927_IR_TMR(tmrnr), TXX9_IMCLK); } void __init tx4927_sio_init(unsigned int sclk, unsigned int cts_mask) { int i; for (i = 0; i < 2; i++) txx9_sio_init(TX4927_SIO_REG(i) & 0xfffffffffULL, TXX9_IRQ_BASE + TX4927_IR_SIO(i), i, sclk, (1 << i) & cts_mask); } void __init tx4927_mtd_init(int ch) { struct physmap_flash_data pdata = { .width = TX4927_EBUSC_WIDTH(ch) / 8, }; unsigned long start = txx9_ce_res[ch].start; unsigned long size = txx9_ce_res[ch].end - start + 1; if (!(TX4927_EBUSC_CR(ch) & 0x8)) return; /* disabled */ txx9_physmap_flash_init(ch, start, size, &pdata); } void __init tx4927_dmac_init(int memcpy_chan) { struct txx9dmac_platform_data plat_data = { .memcpy_chan = memcpy_chan, .have_64bit_regs = true, }; txx9_dmac_init(0, TX4927_DMA_REG & 0xfffffffffULL, TXX9_IRQ_BASE + TX4927_IR_DMA(0), &plat_data); } void __init tx4927_aclc_init(unsigned int dma_chan_out, unsigned int dma_chan_in) { u64 pcfg = __raw_readq(&tx4927_ccfgptr->pcfg); __u64 dmasel_mask = 0, dmasel = 0; unsigned long flags; if (!(pcfg & TX4927_PCFG_SEL2)) return; /* setup DMASEL (playback:ACLC ch0, capture:ACLC ch1) */ switch (dma_chan_out) { case 0: dmasel_mask |= TX4927_PCFG_DMASEL0_MASK; dmasel |= TX4927_PCFG_DMASEL0_ACL0; break; case 2: dmasel_mask |= TX4927_PCFG_DMASEL2_MASK; dmasel |= TX4927_PCFG_DMASEL2_ACL0; break; default: return; } switch (dma_chan_in) { case 1: dmasel_mask |= TX4927_PCFG_DMASEL1_MASK; dmasel |= TX4927_PCFG_DMASEL1_ACL1; break; case 3: dmasel_mask |= TX4927_PCFG_DMASEL3_MASK; dmasel |= TX4927_PCFG_DMASEL3_ACL1; break; default: return; } local_irq_save(flags); txx9_clear64(&tx4927_ccfgptr->pcfg, dmasel_mask); txx9_set64(&tx4927_ccfgptr->pcfg, dmasel); local_irq_restore(flags); txx9_aclc_init(TX4927_ACLC_REG & 0xfffffffffULL, TXX9_IRQ_BASE + TX4927_IR_ACLC, 0, dma_chan_out, dma_chan_in); } static void __init tx4927_stop_unused_modules(void) { __u64 pcfg, rst = 0, ckd = 0; char buf[128]; buf[0] = '\0'; local_irq_disable(); pcfg = ____raw_readq(&tx4927_ccfgptr->pcfg); if (!(pcfg & TX4927_PCFG_SEL2)) { rst |= TX4927_CLKCTR_ACLRST; ckd |= TX4927_CLKCTR_ACLCKD; strcat(buf, " ACLC"); } if (rst | ckd) { txx9_set64(&tx4927_ccfgptr->clkctr, rst); txx9_set64(&tx4927_ccfgptr->clkctr, ckd); } local_irq_enable(); if (buf[0]) pr_info("%s: stop%s\n", txx9_pcode_str, buf); } static int __init tx4927_late_init(void) { if (txx9_pcode != 0x4927) return -ENODEV; tx4927_stop_unused_modules(); return 0; } late_initcall(tx4927_late_init);
linux-master
arch/mips/txx9/generic/setup_tx4927.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2015 Imagination Technologies * Author: Alex Smith <[email protected]> */ /* * This tool is used to generate the real VDSO images from the raw image. It * first patches up the MIPS ABI flags and GNU attributes sections defined in * elf.S to have the correct name and type. It then generates a C source file * to be compiled into the kernel containing the VDSO image data and a * mips_vdso_image struct for it, including symbol offsets extracted from the * image. * * We need to be passed both a stripped and unstripped VDSO image. The stripped * image is compiled into the kernel, but we must also patch up the unstripped * image's ABI flags sections so that it can be installed and used for * debugging. */ #include <sys/mman.h> #include <sys/stat.h> #include <sys/types.h> #include <byteswap.h> #include <elf.h> #include <errno.h> #include <fcntl.h> #include <inttypes.h> #include <stdarg.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> /* Define these in case the system elf.h is not new enough to have them. */ #ifndef SHT_GNU_ATTRIBUTES # define SHT_GNU_ATTRIBUTES 0x6ffffff5 #endif #ifndef SHT_MIPS_ABIFLAGS # define SHT_MIPS_ABIFLAGS 0x7000002a #endif enum { ABI_O32 = (1 << 0), ABI_N32 = (1 << 1), ABI_N64 = (1 << 2), ABI_ALL = ABI_O32 | ABI_N32 | ABI_N64, }; /* Symbols the kernel requires offsets for. */ static struct { const char *name; const char *offset_name; unsigned int abis; } vdso_symbols[] = { { "__vdso_sigreturn", "off_sigreturn", ABI_O32 }, { "__vdso_rt_sigreturn", "off_rt_sigreturn", ABI_ALL }, {} }; static const char *program_name; static const char *vdso_name; static unsigned char elf_class; static unsigned int elf_abi; static bool need_swap; static FILE *out_file; #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ # define HOST_ORDER ELFDATA2LSB #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ # define HOST_ORDER ELFDATA2MSB #endif #define BUILD_SWAP(bits) \ static uint##bits##_t swap_uint##bits(uint##bits##_t val) \ { \ return need_swap ? bswap_##bits(val) : val; \ } BUILD_SWAP(16) BUILD_SWAP(32) BUILD_SWAP(64) #define __FUNC(name, bits) name##bits #define _FUNC(name, bits) __FUNC(name, bits) #define FUNC(name) _FUNC(name, ELF_BITS) #define __ELF(x, bits) Elf##bits##_##x #define _ELF(x, bits) __ELF(x, bits) #define ELF(x) _ELF(x, ELF_BITS) /* * Include genvdso.h twice with ELF_BITS defined differently to get functions * for both ELF32 and ELF64. */ #define ELF_BITS 64 #include "genvdso.h" #undef ELF_BITS #define ELF_BITS 32 #include "genvdso.h" #undef ELF_BITS static void *map_vdso(const char *path, size_t *_size) { int fd; struct stat stat; void *addr; const Elf32_Ehdr *ehdr; fd = open(path, O_RDWR); if (fd < 0) { fprintf(stderr, "%s: Failed to open '%s': %s\n", program_name, path, strerror(errno)); return NULL; } if (fstat(fd, &stat) != 0) { fprintf(stderr, "%s: Failed to stat '%s': %s\n", program_name, path, strerror(errno)); close(fd); return NULL; } addr = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (addr == MAP_FAILED) { fprintf(stderr, "%s: Failed to map '%s': %s\n", program_name, path, strerror(errno)); close(fd); return NULL; } /* ELF32/64 header formats are the same for the bits we're checking. */ ehdr = addr; if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) { fprintf(stderr, "%s: '%s' is not an ELF file\n", program_name, path); close(fd); return NULL; } elf_class = ehdr->e_ident[EI_CLASS]; switch (elf_class) { case ELFCLASS32: case ELFCLASS64: break; default: fprintf(stderr, "%s: '%s' has invalid ELF class\n", program_name, path); close(fd); return NULL; } switch (ehdr->e_ident[EI_DATA]) { case ELFDATA2LSB: case ELFDATA2MSB: need_swap = ehdr->e_ident[EI_DATA] != HOST_ORDER; break; default: fprintf(stderr, "%s: '%s' has invalid ELF data order\n", program_name, path); close(fd); return NULL; } if (swap_uint16(ehdr->e_machine) != EM_MIPS) { fprintf(stderr, "%s: '%s' has invalid ELF machine (expected EM_MIPS)\n", program_name, path); close(fd); return NULL; } else if (swap_uint16(ehdr->e_type) != ET_DYN) { fprintf(stderr, "%s: '%s' has invalid ELF type (expected ET_DYN)\n", program_name, path); close(fd); return NULL; } *_size = stat.st_size; close(fd); return addr; } static bool patch_vdso(const char *path, void *vdso) { if (elf_class == ELFCLASS64) return patch_vdso64(path, vdso); else return patch_vdso32(path, vdso); } static bool get_symbols(const char *path, void *vdso) { if (elf_class == ELFCLASS64) return get_symbols64(path, vdso); else return get_symbols32(path, vdso); } int main(int argc, char **argv) { const char *dbg_vdso_path, *vdso_path, *out_path; void *dbg_vdso, *vdso; size_t dbg_vdso_size, vdso_size, i; program_name = argv[0]; if (argc < 4 || argc > 5) { fprintf(stderr, "Usage: %s <debug VDSO> <stripped VDSO> <output file> [<name>]\n", program_name); return EXIT_FAILURE; } dbg_vdso_path = argv[1]; vdso_path = argv[2]; out_path = argv[3]; vdso_name = (argc > 4) ? argv[4] : ""; dbg_vdso = map_vdso(dbg_vdso_path, &dbg_vdso_size); if (!dbg_vdso) return EXIT_FAILURE; vdso = map_vdso(vdso_path, &vdso_size); if (!vdso) return EXIT_FAILURE; /* Patch both the VDSOs' ABI flags sections. */ if (!patch_vdso(dbg_vdso_path, dbg_vdso)) return EXIT_FAILURE; if (!patch_vdso(vdso_path, vdso)) return EXIT_FAILURE; if (msync(dbg_vdso, dbg_vdso_size, MS_SYNC) != 0) { fprintf(stderr, "%s: Failed to sync '%s': %s\n", program_name, dbg_vdso_path, strerror(errno)); return EXIT_FAILURE; } else if (msync(vdso, vdso_size, MS_SYNC) != 0) { fprintf(stderr, "%s: Failed to sync '%s': %s\n", program_name, vdso_path, strerror(errno)); return EXIT_FAILURE; } out_file = fopen(out_path, "w"); if (!out_file) { fprintf(stderr, "%s: Failed to open '%s': %s\n", program_name, out_path, strerror(errno)); return EXIT_FAILURE; } fprintf(out_file, "/* Automatically generated - do not edit */\n"); fprintf(out_file, "#include <linux/linkage.h>\n"); fprintf(out_file, "#include <linux/mm.h>\n"); fprintf(out_file, "#include <asm/vdso.h>\n"); fprintf(out_file, "static int vdso_mremap(\n"); fprintf(out_file, " const struct vm_special_mapping *sm,\n"); fprintf(out_file, " struct vm_area_struct *new_vma)\n"); fprintf(out_file, "{\n"); fprintf(out_file, " current->mm->context.vdso =\n"); fprintf(out_file, " (void *)(new_vma->vm_start);\n"); fprintf(out_file, " return 0;\n"); fprintf(out_file, "}\n"); /* Write out the stripped VDSO data. */ fprintf(out_file, "static unsigned char vdso_data[PAGE_ALIGN(%zu)] __page_aligned_data = {\n\t", vdso_size); for (i = 0; i < vdso_size; i++) { if (!(i % 10)) fprintf(out_file, "\n\t"); fprintf(out_file, "0x%02x, ", ((unsigned char *)vdso)[i]); } fprintf(out_file, "\n};\n"); /* Preallocate a page array. */ fprintf(out_file, "static struct page *vdso_pages[PAGE_ALIGN(%zu) / PAGE_SIZE];\n", vdso_size); fprintf(out_file, "struct mips_vdso_image vdso_image%s%s = {\n", (vdso_name[0]) ? "_" : "", vdso_name); fprintf(out_file, "\t.data = vdso_data,\n"); fprintf(out_file, "\t.size = PAGE_ALIGN(%zu),\n", vdso_size); fprintf(out_file, "\t.mapping = {\n"); fprintf(out_file, "\t\t.name = \"[vdso]\",\n"); fprintf(out_file, "\t\t.pages = vdso_pages,\n"); fprintf(out_file, "\t\t.mremap = vdso_mremap,\n"); fprintf(out_file, "\t},\n"); /* Calculate and write symbol offsets to <output file> */ if (!get_symbols(dbg_vdso_path, dbg_vdso)) { unlink(out_path); fclose(out_file); return EXIT_FAILURE; } fprintf(out_file, "};\n"); fclose(out_file); return EXIT_SUCCESS; }
linux-master
arch/mips/vdso/genvdso.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * MIPS64 and compat userspace implementations of gettimeofday() * and similar. * * Copyright (C) 2015 Imagination Technologies * Copyright (C) 2018 ARM Limited * */ #include <linux/time.h> #include <linux/types.h> #if _MIPS_SIM != _MIPS_SIM_ABI64 int __vdso_clock_gettime(clockid_t clock, struct old_timespec32 *ts) { return __cvdso_clock_gettime32(clock, ts); } #ifdef CONFIG_MIPS_CLOCK_VSYSCALL /* * This is behind the ifdef so that we don't provide the symbol when there's no * possibility of there being a usable clocksource, because there's nothing we * can do without it. When libc fails the symbol lookup it should fall back on * the standard syscall path. */ int __vdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz) { return __cvdso_gettimeofday(tv, tz); } #endif /* CONFIG_MIPS_CLOCK_VSYSCALL */ int __vdso_clock_getres(clockid_t clock_id, struct old_timespec32 *res) { return __cvdso_clock_getres_time32(clock_id, res); } int __vdso_clock_gettime64(clockid_t clock, struct __kernel_timespec *ts) { return __cvdso_clock_gettime(clock, ts); } #else int __vdso_clock_gettime(clockid_t clock, struct __kernel_timespec *ts) { return __cvdso_clock_gettime(clock, ts); } #ifdef CONFIG_MIPS_CLOCK_VSYSCALL /* * This is behind the ifdef so that we don't provide the symbol when there's no * possibility of there being a usable clocksource, because there's nothing we * can do without it. When libc fails the symbol lookup it should fall back on * the standard syscall path. */ int __vdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz) { return __cvdso_gettimeofday(tv, tz); } #endif /* CONFIG_MIPS_CLOCK_VSYSCALL */ int __vdso_clock_getres(clockid_t clock_id, struct __kernel_timespec *res) { return __cvdso_clock_getres(clock_id, res); } #endif
linux-master
arch/mips/vdso/vgettimeofday.c
// SPDX-License-Identifier: GPL-2.0 /* * Configuration file for O32 and N32 binaries. * Note: To be included before lib/vdso/gettimeofday.c */ #if defined(CONFIG_MIPS32_O32) || defined(CONFIG_MIPS32_N32) /* * In case of a 32 bit VDSO for a 64 bit kernel fake a 32 bit kernel * configuration. */ #undef CONFIG_64BIT #define BUILD_VDSO32 #define CONFIG_32BIT 1 #define CONFIG_GENERIC_ATOMIC64 1 #define BUILD_VDSO32_64 #endif
linux-master
arch/mips/vdso/config-n32-o32-env.c
/* * Copyright (c) 1995 * Ted Lemon (hereinafter referred to as the author) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* elf2ecoff.c This program converts an elf executable to an ECOFF executable. No symbol table is retained. This is useful primarily in building net-bootable kernels for machines (e.g., DECstation and Alpha) which only support the ECOFF object file format. */ #include <stdio.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <elf.h> #include <limits.h> #include <netinet/in.h> #include <stdlib.h> #include <stdint.h> #include <inttypes.h> #include "ecoff.h" /* * Some extra ELF definitions */ #define PT_MIPS_REGINFO 0x70000000 /* Register usage information */ #define PT_MIPS_ABIFLAGS 0x70000003 /* Records ABI related flags */ /* -------------------------------------------------------------------- */ struct sect { uint32_t vaddr; uint32_t len; }; int *symTypeTable; int must_convert_endian; int format_bigendian; static void copy(int out, int in, off_t offset, off_t size) { char ibuf[4096]; int remaining, cur, count; /* Go to the start of the ELF symbol table... */ if (lseek(in, offset, SEEK_SET) < 0) { perror("copy: lseek"); exit(1); } remaining = size; while (remaining) { cur = remaining; if (cur > sizeof ibuf) cur = sizeof ibuf; remaining -= cur; if ((count = read(in, ibuf, cur)) != cur) { fprintf(stderr, "copy: read: %s\n", count ? strerror(errno) : "premature end of file"); exit(1); } if ((count = write(out, ibuf, cur)) != cur) { perror("copy: write"); exit(1); } } } /* * Combine two segments, which must be contiguous. If pad is true, it's * okay for there to be padding between. */ static void combine(struct sect *base, struct sect *new, int pad) { if (!base->len) *base = *new; else if (new->len) { if (base->vaddr + base->len != new->vaddr) { if (pad) base->len = new->vaddr - base->vaddr; else { fprintf(stderr, "Non-contiguous data can't be converted.\n"); exit(1); } } base->len += new->len; } } static int phcmp(const void *v1, const void *v2) { const Elf32_Phdr *h1 = v1; const Elf32_Phdr *h2 = v2; if (h1->p_vaddr > h2->p_vaddr) return 1; else if (h1->p_vaddr < h2->p_vaddr) return -1; else return 0; } static char *saveRead(int file, off_t offset, off_t len, char *name) { char *tmp; int count; off_t off; if ((off = lseek(file, offset, SEEK_SET)) < 0) { fprintf(stderr, "%s: fseek: %s\n", name, strerror(errno)); exit(1); } if (!(tmp = (char *) malloc(len))) { fprintf(stderr, "%s: Can't allocate %ld bytes.\n", name, len); exit(1); } count = read(file, tmp, len); if (count != len) { fprintf(stderr, "%s: read: %s.\n", name, count ? strerror(errno) : "End of file reached"); exit(1); } return tmp; } #define swab16(x) \ ((uint16_t)( \ (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \ (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) )) #define swab32(x) \ ((unsigned int)( \ (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \ (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \ (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \ (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) )) static void convert_elf_hdr(Elf32_Ehdr * e) { e->e_type = swab16(e->e_type); e->e_machine = swab16(e->e_machine); e->e_version = swab32(e->e_version); e->e_entry = swab32(e->e_entry); e->e_phoff = swab32(e->e_phoff); e->e_shoff = swab32(e->e_shoff); e->e_flags = swab32(e->e_flags); e->e_ehsize = swab16(e->e_ehsize); e->e_phentsize = swab16(e->e_phentsize); e->e_phnum = swab16(e->e_phnum); e->e_shentsize = swab16(e->e_shentsize); e->e_shnum = swab16(e->e_shnum); e->e_shstrndx = swab16(e->e_shstrndx); } static void convert_elf_phdrs(Elf32_Phdr * p, int num) { int i; for (i = 0; i < num; i++, p++) { p->p_type = swab32(p->p_type); p->p_offset = swab32(p->p_offset); p->p_vaddr = swab32(p->p_vaddr); p->p_paddr = swab32(p->p_paddr); p->p_filesz = swab32(p->p_filesz); p->p_memsz = swab32(p->p_memsz); p->p_flags = swab32(p->p_flags); p->p_align = swab32(p->p_align); } } static void convert_elf_shdrs(Elf32_Shdr * s, int num) { int i; for (i = 0; i < num; i++, s++) { s->sh_name = swab32(s->sh_name); s->sh_type = swab32(s->sh_type); s->sh_flags = swab32(s->sh_flags); s->sh_addr = swab32(s->sh_addr); s->sh_offset = swab32(s->sh_offset); s->sh_size = swab32(s->sh_size); s->sh_link = swab32(s->sh_link); s->sh_info = swab32(s->sh_info); s->sh_addralign = swab32(s->sh_addralign); s->sh_entsize = swab32(s->sh_entsize); } } static void convert_ecoff_filehdr(struct filehdr *f) { f->f_magic = swab16(f->f_magic); f->f_nscns = swab16(f->f_nscns); f->f_timdat = swab32(f->f_timdat); f->f_symptr = swab32(f->f_symptr); f->f_nsyms = swab32(f->f_nsyms); f->f_opthdr = swab16(f->f_opthdr); f->f_flags = swab16(f->f_flags); } static void convert_ecoff_aouthdr(struct aouthdr *a) { a->magic = swab16(a->magic); a->vstamp = swab16(a->vstamp); a->tsize = swab32(a->tsize); a->dsize = swab32(a->dsize); a->bsize = swab32(a->bsize); a->entry = swab32(a->entry); a->text_start = swab32(a->text_start); a->data_start = swab32(a->data_start); a->bss_start = swab32(a->bss_start); a->gprmask = swab32(a->gprmask); a->cprmask[0] = swab32(a->cprmask[0]); a->cprmask[1] = swab32(a->cprmask[1]); a->cprmask[2] = swab32(a->cprmask[2]); a->cprmask[3] = swab32(a->cprmask[3]); a->gp_value = swab32(a->gp_value); } static void convert_ecoff_esecs(struct scnhdr *s, int num) { int i; for (i = 0; i < num; i++, s++) { s->s_paddr = swab32(s->s_paddr); s->s_vaddr = swab32(s->s_vaddr); s->s_size = swab32(s->s_size); s->s_scnptr = swab32(s->s_scnptr); s->s_relptr = swab32(s->s_relptr); s->s_lnnoptr = swab32(s->s_lnnoptr); s->s_nreloc = swab16(s->s_nreloc); s->s_nlnno = swab16(s->s_nlnno); s->s_flags = swab32(s->s_flags); } } int main(int argc, char *argv[]) { Elf32_Ehdr ex; Elf32_Phdr *ph; Elf32_Shdr *sh; int i, pad; struct sect text, data, bss; struct filehdr efh; struct aouthdr eah; struct scnhdr esecs[6]; int infile, outfile; uint32_t cur_vma = UINT32_MAX; int addflag = 0; int nosecs; text.len = data.len = bss.len = 0; text.vaddr = data.vaddr = bss.vaddr = 0; /* Check args... */ if (argc < 3 || argc > 4) { usage: fprintf(stderr, "usage: elf2ecoff <elf executable> <ecoff executable> [-a]\n"); exit(1); } if (argc == 4) { if (strcmp(argv[3], "-a")) goto usage; addflag = 1; } /* Try the input file... */ if ((infile = open(argv[1], O_RDONLY)) < 0) { fprintf(stderr, "Can't open %s for read: %s\n", argv[1], strerror(errno)); exit(1); } /* Read the header, which is at the beginning of the file... */ i = read(infile, &ex, sizeof ex); if (i != sizeof ex) { fprintf(stderr, "ex: %s: %s.\n", argv[1], i ? strerror(errno) : "End of file reached"); exit(1); } if (ex.e_ident[EI_DATA] == ELFDATA2MSB) format_bigendian = 1; if (ntohs(0xaa55) == 0xaa55) { if (!format_bigendian) must_convert_endian = 1; } else { if (format_bigendian) must_convert_endian = 1; } if (must_convert_endian) convert_elf_hdr(&ex); /* Read the program headers... */ ph = (Elf32_Phdr *) saveRead(infile, ex.e_phoff, ex.e_phnum * sizeof(Elf32_Phdr), "ph"); if (must_convert_endian) convert_elf_phdrs(ph, ex.e_phnum); /* Read the section headers... */ sh = (Elf32_Shdr *) saveRead(infile, ex.e_shoff, ex.e_shnum * sizeof(Elf32_Shdr), "sh"); if (must_convert_endian) convert_elf_shdrs(sh, ex.e_shnum); /* Figure out if we can cram the program header into an ECOFF header... Basically, we can't handle anything but loadable segments, but we can ignore some kinds of segments. We can't handle holes in the address space. Segments may be out of order, so we sort them first. */ qsort(ph, ex.e_phnum, sizeof(Elf32_Phdr), phcmp); for (i = 0; i < ex.e_phnum; i++) { /* Section types we can ignore... */ switch (ph[i].p_type) { case PT_NULL: case PT_NOTE: case PT_PHDR: case PT_MIPS_REGINFO: case PT_MIPS_ABIFLAGS: continue; case PT_LOAD: /* Writable (data) segment? */ if (ph[i].p_flags & PF_W) { struct sect ndata, nbss; ndata.vaddr = ph[i].p_vaddr; ndata.len = ph[i].p_filesz; nbss.vaddr = ph[i].p_vaddr + ph[i].p_filesz; nbss.len = ph[i].p_memsz - ph[i].p_filesz; combine(&data, &ndata, 0); combine(&bss, &nbss, 1); } else { struct sect ntxt; ntxt.vaddr = ph[i].p_vaddr; ntxt.len = ph[i].p_filesz; combine(&text, &ntxt, 0); } /* Remember the lowest segment start address. */ if (ph[i].p_vaddr < cur_vma) cur_vma = ph[i].p_vaddr; break; default: /* Section types we can't handle... */ fprintf(stderr, "Program header %d type %d can't be converted.\n", ex.e_phnum, ph[i].p_type); exit(1); } } /* Sections must be in order to be converted... */ if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr || text.vaddr + text.len > data.vaddr || data.vaddr + data.len > bss.vaddr) { fprintf(stderr, "Sections ordering prevents a.out conversion.\n"); exit(1); } /* If there's a data section but no text section, then the loader combined everything into one section. That needs to be the text section, so just make the data section zero length following text. */ if (data.len && !text.len) { text = data; data.vaddr = text.vaddr + text.len; data.len = 0; } /* If there is a gap between text and data, we'll fill it when we copy the data, so update the length of the text segment as represented in a.out to reflect that, since a.out doesn't allow gaps in the program address space. */ if (text.vaddr + text.len < data.vaddr) text.len = data.vaddr - text.vaddr; /* We now have enough information to cons up an a.out header... */ eah.magic = OMAGIC; eah.vstamp = 200; eah.tsize = text.len; eah.dsize = data.len; eah.bsize = bss.len; eah.entry = ex.e_entry; eah.text_start = text.vaddr; eah.data_start = data.vaddr; eah.bss_start = bss.vaddr; eah.gprmask = 0xf3fffffe; memset(&eah.cprmask, '\0', sizeof eah.cprmask); eah.gp_value = 0; /* unused. */ if (format_bigendian) efh.f_magic = MIPSEBMAGIC; else efh.f_magic = MIPSELMAGIC; if (addflag) nosecs = 6; else nosecs = 3; efh.f_nscns = nosecs; efh.f_timdat = 0; /* bogus */ efh.f_symptr = 0; efh.f_nsyms = 0; efh.f_opthdr = sizeof eah; efh.f_flags = 0x100f; /* Stripped, not sharable. */ memset(esecs, 0, sizeof esecs); strcpy(esecs[0].s_name, ".text"); strcpy(esecs[1].s_name, ".data"); strcpy(esecs[2].s_name, ".bss"); if (addflag) { strcpy(esecs[3].s_name, ".rdata"); strcpy(esecs[4].s_name, ".sdata"); strcpy(esecs[5].s_name, ".sbss"); } esecs[0].s_paddr = esecs[0].s_vaddr = eah.text_start; esecs[1].s_paddr = esecs[1].s_vaddr = eah.data_start; esecs[2].s_paddr = esecs[2].s_vaddr = eah.bss_start; if (addflag) { esecs[3].s_paddr = esecs[3].s_vaddr = 0; esecs[4].s_paddr = esecs[4].s_vaddr = 0; esecs[5].s_paddr = esecs[5].s_vaddr = 0; } esecs[0].s_size = eah.tsize; esecs[1].s_size = eah.dsize; esecs[2].s_size = eah.bsize; if (addflag) { esecs[3].s_size = 0; esecs[4].s_size = 0; esecs[5].s_size = 0; } esecs[0].s_scnptr = N_TXTOFF(efh, eah); esecs[1].s_scnptr = N_DATOFF(efh, eah); #define ECOFF_SEGMENT_ALIGNMENT(a) 0x10 #define ECOFF_ROUND(s, a) (((s)+(a)-1)&~((a)-1)) esecs[2].s_scnptr = esecs[1].s_scnptr + ECOFF_ROUND(esecs[1].s_size, ECOFF_SEGMENT_ALIGNMENT(&eah)); if (addflag) { esecs[3].s_scnptr = 0; esecs[4].s_scnptr = 0; esecs[5].s_scnptr = 0; } esecs[0].s_relptr = esecs[1].s_relptr = esecs[2].s_relptr = 0; esecs[0].s_lnnoptr = esecs[1].s_lnnoptr = esecs[2].s_lnnoptr = 0; esecs[0].s_nreloc = esecs[1].s_nreloc = esecs[2].s_nreloc = 0; esecs[0].s_nlnno = esecs[1].s_nlnno = esecs[2].s_nlnno = 0; if (addflag) { esecs[3].s_relptr = esecs[4].s_relptr = esecs[5].s_relptr = 0; esecs[3].s_lnnoptr = esecs[4].s_lnnoptr = esecs[5].s_lnnoptr = 0; esecs[3].s_nreloc = esecs[4].s_nreloc = esecs[5].s_nreloc = 0; esecs[3].s_nlnno = esecs[4].s_nlnno = esecs[5].s_nlnno = 0; } esecs[0].s_flags = 0x20; esecs[1].s_flags = 0x40; esecs[2].s_flags = 0x82; if (addflag) { esecs[3].s_flags = 0x100; esecs[4].s_flags = 0x200; esecs[5].s_flags = 0x400; } /* Make the output file... */ if ((outfile = open(argv[2], O_WRONLY | O_CREAT, 0777)) < 0) { fprintf(stderr, "Unable to create %s: %s\n", argv[2], strerror(errno)); exit(1); } if (must_convert_endian) convert_ecoff_filehdr(&efh); /* Write the headers... */ i = write(outfile, &efh, sizeof efh); if (i != sizeof efh) { perror("efh: write"); exit(1); for (i = 0; i < nosecs; i++) { printf ("Section %d: %s phys %"PRIx32" size %"PRIx32"\t file offset %"PRIx32"\n", i, esecs[i].s_name, esecs[i].s_paddr, esecs[i].s_size, esecs[i].s_scnptr); } } fprintf(stderr, "wrote %d byte file header.\n", i); if (must_convert_endian) convert_ecoff_aouthdr(&eah); i = write(outfile, &eah, sizeof eah); if (i != sizeof eah) { perror("eah: write"); exit(1); } fprintf(stderr, "wrote %d byte a.out header.\n", i); if (must_convert_endian) convert_ecoff_esecs(&esecs[0], nosecs); i = write(outfile, &esecs, nosecs * sizeof(struct scnhdr)); if (i != nosecs * sizeof(struct scnhdr)) { perror("esecs: write"); exit(1); } fprintf(stderr, "wrote %d bytes of section headers.\n", i); pad = (sizeof(efh) + sizeof(eah) + nosecs * sizeof(struct scnhdr)) & 15; if (pad) { pad = 16 - pad; i = write(outfile, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0", pad); if (i < 0) { perror("ipad: write"); exit(1); } fprintf(stderr, "wrote %d byte pad.\n", i); } /* * Copy the loadable sections. Zero-fill any gaps less than 64k; * complain about any zero-filling, and die if we're asked to zero-fill * more than 64k. */ for (i = 0; i < ex.e_phnum; i++) { /* Unprocessable sections were handled above, so just verify that the section can be loaded before copying. */ if (ph[i].p_type == PT_LOAD && ph[i].p_filesz) { if (cur_vma != ph[i].p_vaddr) { uint32_t gap = ph[i].p_vaddr - cur_vma; char obuf[1024]; if (gap > 65536) { fprintf(stderr, "Intersegment gap (%"PRId32" bytes) too large.\n", gap); exit(1); } fprintf(stderr, "Warning: %d byte intersegment gap.\n", gap); memset(obuf, 0, sizeof obuf); while (gap) { int count = write(outfile, obuf, (gap > sizeof obuf ? sizeof obuf : gap)); if (count < 0) { fprintf(stderr, "Error writing gap: %s\n", strerror(errno)); exit(1); } gap -= count; } } fprintf(stderr, "writing %d bytes...\n", ph[i].p_filesz); copy(outfile, infile, ph[i].p_offset, ph[i].p_filesz); cur_vma = ph[i].p_vaddr + ph[i].p_filesz; } } /* * Write a page of padding for boot PROMS that read entire pages. * Without this, they may attempt to read past the end of the * data section, incur an error, and refuse to boot. */ { char obuf[4096]; memset(obuf, 0, sizeof obuf); if (write(outfile, obuf, sizeof(obuf)) != sizeof(obuf)) { fprintf(stderr, "Error writing PROM padding: %s\n", strerror(errno)); exit(1); } } /* Looks like we won... */ exit(0); }
linux-master
arch/mips/boot/elf2ecoff.c
// SPDX-License-Identifier: GPL-2.0 #include "relocs.h" #define ELF_BITS 64 #define ELF_MACHINE EM_MIPS #define ELF_MACHINE_NAME "MIPS64" #define SHT_REL_TYPE SHT_RELA #define Elf_Rel Elf64_Rela typedef uint8_t Elf64_Byte; typedef union { struct { Elf64_Word r_sym; /* Symbol index. */ Elf64_Byte r_ssym; /* Special symbol. */ Elf64_Byte r_type3; /* Third relocation. */ Elf64_Byte r_type2; /* Second relocation. */ Elf64_Byte r_type; /* First relocation. */ } fields; Elf64_Xword unused; } Elf64_Mips_Rela; #define ELF_CLASS ELFCLASS64 #define ELF_R_SYM(val) (((Elf64_Mips_Rela *)(&val))->fields.r_sym) #define ELF_R_TYPE(val) (((Elf64_Mips_Rela *)(&val))->fields.r_type) #define ELF_ST_TYPE(o) ELF64_ST_TYPE(o) #define ELF_ST_BIND(o) ELF64_ST_BIND(o) #define ELF_ST_VISIBILITY(o) ELF64_ST_VISIBILITY(o) #include "relocs.c"
linux-master
arch/mips/boot/tools/relocs_64.c
// SPDX-License-Identifier: GPL-2.0 /* This is included from relocs_32/64.c */ #define ElfW(type) _ElfW(ELF_BITS, type) #define _ElfW(bits, type) __ElfW(bits, type) #define __ElfW(bits, type) Elf##bits##_##type #define Elf_Addr ElfW(Addr) #define Elf_Ehdr ElfW(Ehdr) #define Elf_Phdr ElfW(Phdr) #define Elf_Shdr ElfW(Shdr) #define Elf_Sym ElfW(Sym) static Elf_Ehdr ehdr; struct relocs { uint32_t *offset; unsigned long count; unsigned long size; }; static struct relocs relocs; struct section { Elf_Shdr shdr; struct section *link; Elf_Sym *symtab; Elf_Rel *reltab; char *strtab; long shdr_offset; }; static struct section *secs; static const char * const regex_sym_kernel = { /* Symbols matching these regex's should never be relocated */ "^(__crc_)", }; static regex_t sym_regex_c; static int regex_skip_reloc(const char *sym_name) { return !regexec(&sym_regex_c, sym_name, 0, NULL, 0); } static void regex_init(void) { char errbuf[128]; int err; err = regcomp(&sym_regex_c, regex_sym_kernel, REG_EXTENDED|REG_NOSUB); if (err) { regerror(err, &sym_regex_c, errbuf, sizeof(errbuf)); die("%s", errbuf); } } static const char *rel_type(unsigned type) { static const char * const type_name[] = { #define REL_TYPE(X)[X] = #X REL_TYPE(R_MIPS_NONE), REL_TYPE(R_MIPS_16), REL_TYPE(R_MIPS_32), REL_TYPE(R_MIPS_REL32), REL_TYPE(R_MIPS_26), REL_TYPE(R_MIPS_HI16), REL_TYPE(R_MIPS_LO16), REL_TYPE(R_MIPS_GPREL16), REL_TYPE(R_MIPS_LITERAL), REL_TYPE(R_MIPS_GOT16), REL_TYPE(R_MIPS_PC16), REL_TYPE(R_MIPS_CALL16), REL_TYPE(R_MIPS_GPREL32), REL_TYPE(R_MIPS_64), REL_TYPE(R_MIPS_HIGHER), REL_TYPE(R_MIPS_HIGHEST), REL_TYPE(R_MIPS_PC21_S2), REL_TYPE(R_MIPS_PC26_S2), #undef REL_TYPE }; const char *name = "unknown type rel type name"; if (type < ARRAY_SIZE(type_name) && type_name[type]) name = type_name[type]; return name; } static const char *sec_name(unsigned shndx) { const char *sec_strtab; const char *name; sec_strtab = secs[ehdr.e_shstrndx].strtab; if (shndx < ehdr.e_shnum) name = sec_strtab + secs[shndx].shdr.sh_name; else if (shndx == SHN_ABS) name = "ABSOLUTE"; else if (shndx == SHN_COMMON) name = "COMMON"; else name = "<noname>"; return name; } static struct section *sec_lookup(const char *secname) { int i; for (i = 0; i < ehdr.e_shnum; i++) if (strcmp(secname, sec_name(i)) == 0) return &secs[i]; return NULL; } static const char *sym_name(const char *sym_strtab, Elf_Sym *sym) { const char *name; if (sym->st_name) name = sym_strtab + sym->st_name; else name = sec_name(sym->st_shndx); return name; } #if BYTE_ORDER == LITTLE_ENDIAN #define le16_to_cpu(val) (val) #define le32_to_cpu(val) (val) #define le64_to_cpu(val) (val) #define be16_to_cpu(val) bswap_16(val) #define be32_to_cpu(val) bswap_32(val) #define be64_to_cpu(val) bswap_64(val) #define cpu_to_le16(val) (val) #define cpu_to_le32(val) (val) #define cpu_to_le64(val) (val) #define cpu_to_be16(val) bswap_16(val) #define cpu_to_be32(val) bswap_32(val) #define cpu_to_be64(val) bswap_64(val) #endif #if BYTE_ORDER == BIG_ENDIAN #define le16_to_cpu(val) bswap_16(val) #define le32_to_cpu(val) bswap_32(val) #define le64_to_cpu(val) bswap_64(val) #define be16_to_cpu(val) (val) #define be32_to_cpu(val) (val) #define be64_to_cpu(val) (val) #define cpu_to_le16(val) bswap_16(val) #define cpu_to_le32(val) bswap_32(val) #define cpu_to_le64(val) bswap_64(val) #define cpu_to_be16(val) (val) #define cpu_to_be32(val) (val) #define cpu_to_be64(val) (val) #endif static uint16_t elf16_to_cpu(uint16_t val) { if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) return le16_to_cpu(val); else return be16_to_cpu(val); } static uint32_t elf32_to_cpu(uint32_t val) { if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) return le32_to_cpu(val); else return be32_to_cpu(val); } static uint32_t cpu_to_elf32(uint32_t val) { if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) return cpu_to_le32(val); else return cpu_to_be32(val); } #define elf_half_to_cpu(x) elf16_to_cpu(x) #define elf_word_to_cpu(x) elf32_to_cpu(x) #if ELF_BITS == 64 static uint64_t elf64_to_cpu(uint64_t val) { if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) return le64_to_cpu(val); else return be64_to_cpu(val); } #define elf_addr_to_cpu(x) elf64_to_cpu(x) #define elf_off_to_cpu(x) elf64_to_cpu(x) #define elf_xword_to_cpu(x) elf64_to_cpu(x) #else #define elf_addr_to_cpu(x) elf32_to_cpu(x) #define elf_off_to_cpu(x) elf32_to_cpu(x) #define elf_xword_to_cpu(x) elf32_to_cpu(x) #endif static void read_ehdr(FILE *fp) { if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) die("Cannot read ELF header: %s\n", strerror(errno)); if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) die("No ELF magic\n"); if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) die("Not a %d bit executable\n", ELF_BITS); if ((ehdr.e_ident[EI_DATA] != ELFDATA2LSB) && (ehdr.e_ident[EI_DATA] != ELFDATA2MSB)) die("Unknown ELF Endianness\n"); if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) die("Unknown ELF version\n"); /* Convert the fields to native endian */ ehdr.e_type = elf_half_to_cpu(ehdr.e_type); ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine); ehdr.e_version = elf_word_to_cpu(ehdr.e_version); ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry); ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff); ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff); ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags); ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize); ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize); ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum); ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize); ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum); ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx); if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) die("Unsupported ELF header type\n"); if (ehdr.e_machine != ELF_MACHINE) die("Not for %s\n", ELF_MACHINE_NAME); if (ehdr.e_version != EV_CURRENT) die("Unknown ELF version\n"); if (ehdr.e_ehsize != sizeof(Elf_Ehdr)) die("Bad ELF header size\n"); if (ehdr.e_phentsize != sizeof(Elf_Phdr)) die("Bad program header entry\n"); if (ehdr.e_shentsize != sizeof(Elf_Shdr)) die("Bad section header entry\n"); if (ehdr.e_shstrndx >= ehdr.e_shnum) die("String table index out of bounds\n"); } static void read_shdrs(FILE *fp) { int i; Elf_Shdr shdr; secs = calloc(ehdr.e_shnum, sizeof(struct section)); if (!secs) die("Unable to allocate %d section headers\n", ehdr.e_shnum); if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno)); for (i = 0; i < ehdr.e_shnum; i++) { struct section *sec = &secs[i]; sec->shdr_offset = ftell(fp); if (fread(&shdr, sizeof(shdr), 1, fp) != 1) die("Cannot read ELF section headers %d/%d: %s\n", i, ehdr.e_shnum, strerror(errno)); sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name); sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type); sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags); sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr); sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset); sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size); sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link); sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info); sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign); sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize); if (sec->shdr.sh_link < ehdr.e_shnum) sec->link = &secs[sec->shdr.sh_link]; } } static void read_strtabs(FILE *fp) { int i; for (i = 0; i < ehdr.e_shnum; i++) { struct section *sec = &secs[i]; if (sec->shdr.sh_type != SHT_STRTAB) continue; sec->strtab = malloc(sec->shdr.sh_size); if (!sec->strtab) die("malloc of %d bytes for strtab failed\n", sec->shdr.sh_size); if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) die("Seek to %d failed: %s\n", sec->shdr.sh_offset, strerror(errno)); if (fread(sec->strtab, 1, sec->shdr.sh_size, fp) != sec->shdr.sh_size) die("Cannot read symbol table: %s\n", strerror(errno)); } } static void read_symtabs(FILE *fp) { int i, j; for (i = 0; i < ehdr.e_shnum; i++) { struct section *sec = &secs[i]; if (sec->shdr.sh_type != SHT_SYMTAB) continue; sec->symtab = malloc(sec->shdr.sh_size); if (!sec->symtab) die("malloc of %d bytes for symtab failed\n", sec->shdr.sh_size); if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) die("Seek to %d failed: %s\n", sec->shdr.sh_offset, strerror(errno)); if (fread(sec->symtab, 1, sec->shdr.sh_size, fp) != sec->shdr.sh_size) die("Cannot read symbol table: %s\n", strerror(errno)); for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) { Elf_Sym *sym = &sec->symtab[j]; sym->st_name = elf_word_to_cpu(sym->st_name); sym->st_value = elf_addr_to_cpu(sym->st_value); sym->st_size = elf_xword_to_cpu(sym->st_size); sym->st_shndx = elf_half_to_cpu(sym->st_shndx); } } } static void read_relocs(FILE *fp) { static unsigned long base; int i, j; if (!base) { struct section *sec = sec_lookup(".text"); if (!sec) die("Could not find .text section\n"); base = sec->shdr.sh_addr; } for (i = 0; i < ehdr.e_shnum; i++) { struct section *sec = &secs[i]; if (sec->shdr.sh_type != SHT_REL_TYPE) continue; sec->reltab = malloc(sec->shdr.sh_size); if (!sec->reltab) die("malloc of %d bytes for relocs failed\n", sec->shdr.sh_size); if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) die("Seek to %d failed: %s\n", sec->shdr.sh_offset, strerror(errno)); if (fread(sec->reltab, 1, sec->shdr.sh_size, fp) != sec->shdr.sh_size) die("Cannot read symbol table: %s\n", strerror(errno)); for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) { Elf_Rel *rel = &sec->reltab[j]; rel->r_offset = elf_addr_to_cpu(rel->r_offset); /* Set offset into kernel image */ rel->r_offset -= base; #if (ELF_BITS == 32) rel->r_info = elf_xword_to_cpu(rel->r_info); #else /* Convert MIPS64 RELA format - only the symbol * index needs converting to native endianness */ rel->r_info = rel->r_info; ELF_R_SYM(rel->r_info) = elf32_to_cpu(ELF_R_SYM(rel->r_info)); #endif #if (SHT_REL_TYPE == SHT_RELA) rel->r_addend = elf_xword_to_cpu(rel->r_addend); #endif } } } static void remove_relocs(FILE *fp) { int i; Elf_Shdr shdr; for (i = 0; i < ehdr.e_shnum; i++) { struct section *sec = &secs[i]; if (sec->shdr.sh_type != SHT_REL_TYPE) continue; if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0) die("Seek to %d failed: %s\n", sec->shdr_offset, strerror(errno)); if (fread(&shdr, sizeof(shdr), 1, fp) != 1) die("Cannot read ELF section headers %d/%d: %s\n", i, ehdr.e_shnum, strerror(errno)); /* Set relocation section size to 0, effectively removing it. * This is necessary due to lack of support for relocations * in objcopy when creating 32bit elf from 64bit elf. */ shdr.sh_size = 0; if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0) die("Seek to %d failed: %s\n", sec->shdr_offset, strerror(errno)); if (fwrite(&shdr, sizeof(shdr), 1, fp) != 1) die("Cannot write ELF section headers %d/%d: %s\n", i, ehdr.e_shnum, strerror(errno)); } } static void add_reloc(struct relocs *r, uint32_t offset, unsigned type) { /* Relocation representation in binary table: * |76543210|76543210|76543210|76543210| * | Type | offset from _text >> 2 | */ offset >>= 2; if (offset > 0x00FFFFFF) die("Kernel image exceeds maximum size for relocation!\n"); offset = (offset & 0x00FFFFFF) | ((type & 0xFF) << 24); if (r->count == r->size) { unsigned long newsize = r->size + 50000; void *mem = realloc(r->offset, newsize * sizeof(r->offset[0])); if (!mem) die("realloc failed\n"); r->offset = mem; r->size = newsize; } r->offset[r->count++] = offset; } static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, Elf_Sym *sym, const char *symname)) { int i; /* Walk through the relocations */ for (i = 0; i < ehdr.e_shnum; i++) { char *sym_strtab; Elf_Sym *sh_symtab; struct section *sec_applies, *sec_symtab; int j; struct section *sec = &secs[i]; if (sec->shdr.sh_type != SHT_REL_TYPE) continue; sec_symtab = sec->link; sec_applies = &secs[sec->shdr.sh_info]; if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) continue; sh_symtab = sec_symtab->symtab; sym_strtab = sec_symtab->link->strtab; for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) { Elf_Rel *rel = &sec->reltab[j]; Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)]; const char *symname = sym_name(sym_strtab, sym); process(sec, rel, sym, symname); } } } static int do_reloc(struct section *sec, Elf_Rel *rel, Elf_Sym *sym, const char *symname) { unsigned r_type = ELF_R_TYPE(rel->r_info); unsigned bind = ELF_ST_BIND(sym->st_info); if ((bind == STB_WEAK) && (sym->st_value == 0)) { /* Don't relocate weak symbols without a target */ return 0; } if (regex_skip_reloc(symname)) return 0; switch (r_type) { case R_MIPS_NONE: case R_MIPS_REL32: case R_MIPS_PC16: case R_MIPS_PC21_S2: case R_MIPS_PC26_S2: /* * NONE can be ignored and PC relative relocations don't * need to be adjusted. */ case R_MIPS_HIGHEST: case R_MIPS_HIGHER: /* We support relocating within the same 4Gb segment only, * thus leaving the top 32bits unchanged */ case R_MIPS_LO16: /* We support relocating by 64k jumps only * thus leaving the bottom 16bits unchanged */ break; case R_MIPS_64: case R_MIPS_32: case R_MIPS_26: case R_MIPS_HI16: add_reloc(&relocs, rel->r_offset, r_type); break; default: die("Unsupported relocation type: %s (%d)\n", rel_type(r_type), r_type); break; } return 0; } static int write_reloc_as_bin(uint32_t v, FILE *f) { unsigned char buf[4]; v = cpu_to_elf32(v); memcpy(buf, &v, sizeof(uint32_t)); return fwrite(buf, 1, 4, f); } static int write_reloc_as_text(uint32_t v, FILE *f) { int res; res = fprintf(f, "\t.long 0x%08"PRIx32"\n", v); if (res < 0) return res; else return sizeof(uint32_t); } static void emit_relocs(int as_text, int as_bin, FILE *outf) { int i; int (*write_reloc)(uint32_t, FILE *) = write_reloc_as_bin; int size = 0; int size_reserved; struct section *sec_reloc; sec_reloc = sec_lookup(".data.reloc"); if (!sec_reloc) die("Could not find relocation section\n"); size_reserved = sec_reloc->shdr.sh_size; /* Collect up the relocations */ walk_relocs(do_reloc); /* Print the relocations */ if (as_text) { /* Print the relocations in a form suitable that * gas will like. */ printf(".section \".data.reloc\",\"a\"\n"); printf(".balign 4\n"); /* Output text to stdout */ write_reloc = write_reloc_as_text; outf = stdout; } else if (as_bin) { /* Output raw binary to stdout */ outf = stdout; } else { /* Seek to offset of the relocation section. * Each relocation is then written into the * vmlinux kernel image. */ if (fseek(outf, sec_reloc->shdr.sh_offset, SEEK_SET) < 0) { die("Seek to %d failed: %s\n", sec_reloc->shdr.sh_offset, strerror(errno)); } } for (i = 0; i < relocs.count; i++) size += write_reloc(relocs.offset[i], outf); /* Print a stop, but only if we've actually written some relocs */ if (size) size += write_reloc(0, outf); if (size > size_reserved) /* Die, but suggest a value for CONFIG_RELOCATION_TABLE_SIZE * which will fix this problem and allow a bit of headroom * if more kernel features are enabled */ die("Relocations overflow available space!\n" \ "Please adjust CONFIG_RELOCATION_TABLE_SIZE " \ "to at least 0x%08x\n", (size + 0x1000) & ~0xFFF); } /* * As an aid to debugging problems with different linkers * print summary information about the relocs. * Since different linkers tend to emit the sections in * different orders we use the section names in the output. */ static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, const char *symname) { printf("%16s 0x%08x %16s %40s %16s\n", sec_name(sec->shdr.sh_info), (unsigned int)rel->r_offset, rel_type(ELF_R_TYPE(rel->r_info)), symname, sec_name(sym->st_shndx)); return 0; } static void print_reloc_info(void) { printf("%16s %10s %16s %40s %16s\n", "reloc section", "offset", "reloc type", "symbol", "symbol section"); walk_relocs(do_reloc_info); } #if ELF_BITS == 64 # define process process_64 #else # define process process_32 #endif void process(FILE *fp, int as_text, int as_bin, int show_reloc_info, int keep_relocs) { regex_init(); read_ehdr(fp); read_shdrs(fp); read_strtabs(fp); read_symtabs(fp); read_relocs(fp); if (show_reloc_info) { print_reloc_info(); return; } emit_relocs(as_text, as_bin, fp); if (!keep_relocs) remove_relocs(fp); }
linux-master
arch/mips/boot/tools/relocs.c
// SPDX-License-Identifier: GPL-2.0 #include <stdio.h> #include <stdint.h> #include <stdarg.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <endian.h> #include <elf.h> #include "relocs.h" void die(char *fmt, ...) { va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); exit(1); } static void usage(void) { die("relocs [--reloc-info|--text|--bin|--keep] vmlinux\n"); } int main(int argc, char **argv) { int show_reloc_info, as_text, as_bin, keep_relocs; const char *fname; FILE *fp; int i; unsigned char e_ident[EI_NIDENT]; show_reloc_info = 0; as_text = 0; as_bin = 0; keep_relocs = 0; fname = NULL; for (i = 1; i < argc; i++) { char *arg = argv[i]; if (*arg == '-') { if (strcmp(arg, "--reloc-info") == 0) { show_reloc_info = 1; continue; } if (strcmp(arg, "--text") == 0) { as_text = 1; continue; } if (strcmp(arg, "--bin") == 0) { as_bin = 1; continue; } if (strcmp(arg, "--keep") == 0) { keep_relocs = 1; continue; } } else if (!fname) { fname = arg; continue; } usage(); } if (!fname) usage(); fp = fopen(fname, "r+"); if (!fp) die("Cannot open %s: %s\n", fname, strerror(errno)); if (fread(&e_ident, 1, EI_NIDENT, fp) != EI_NIDENT) die("Cannot read %s: %s", fname, strerror(errno)); rewind(fp); if (e_ident[EI_CLASS] == ELFCLASS64) process_64(fp, as_text, as_bin, show_reloc_info, keep_relocs); else process_32(fp, as_text, as_bin, show_reloc_info, keep_relocs); fclose(fp); return 0; }
linux-master
arch/mips/boot/tools/relocs_main.c
// SPDX-License-Identifier: GPL-2.0 #include "relocs.h" #define ELF_BITS 32 #define ELF_MACHINE EM_MIPS #define ELF_MACHINE_NAME "MIPS" #define SHT_REL_TYPE SHT_REL #define Elf_Rel ElfW(Rel) #define ELF_CLASS ELFCLASS32 #define ELF_R_SYM(val) ELF32_R_SYM(val) #define ELF_R_TYPE(val) ELF32_R_TYPE(val) #define ELF_ST_TYPE(o) ELF32_ST_TYPE(o) #define ELF_ST_BIND(o) ELF32_ST_BIND(o) #define ELF_ST_VISIBILITY(o) ELF32_ST_VISIBILITY(o) #include "relocs.c"
linux-master
arch/mips/boot/tools/relocs_32.c
// SPDX-License-Identifier: GPL-2.0-only #include "../../lib/bswapsi.c"
linux-master
arch/mips/boot/compressed/bswapsi.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright 2001 MontaVista Software Inc. * Author: Matt Porter <[email protected]> * * Copyright (C) 2009 Lemote, Inc. * Author: Wu Zhangjin <[email protected]> */ #define DISABLE_BRANCH_PROFILING #define __NO_FORTIFY #include <linux/types.h> #include <linux/kernel.h> #include <linux/string.h> #include <linux/libfdt.h> #include <asm/addrspace.h> #include <asm/unaligned.h> #include <asm-generic/vmlinux.lds.h> /* * These two variables specify the free mem region * that can be used for temporary malloc area */ unsigned long free_mem_ptr; unsigned long free_mem_end_ptr; /* The linker tells us where the image is. */ extern unsigned char __image_begin[], __image_end[]; /* debug interfaces */ #ifdef CONFIG_DEBUG_ZBOOT extern void puts(const char *s); extern void puthex(unsigned long long val); #else #define puts(s) do {} while (0) #define puthex(val) do {} while (0) #endif extern char __appended_dtb[]; void error(char *x) { puts("\n\n"); puts(x); puts("\n\n -- System halted"); while (1) ; /* Halt */ } /* activate the code for pre-boot environment */ #define STATIC static #ifdef CONFIG_KERNEL_GZIP #include "../../../../lib/decompress_inflate.c" #endif #ifdef CONFIG_KERNEL_BZIP2 #include "../../../../lib/decompress_bunzip2.c" #endif #ifdef CONFIG_KERNEL_LZ4 #include "../../../../lib/decompress_unlz4.c" #endif #ifdef CONFIG_KERNEL_LZMA #include "../../../../lib/decompress_unlzma.c" #endif #ifdef CONFIG_KERNEL_LZO #include "../../../../lib/decompress_unlzo.c" #endif #ifdef CONFIG_KERNEL_XZ #include "../../../../lib/decompress_unxz.c" #endif #ifdef CONFIG_KERNEL_ZSTD #include "../../../../lib/decompress_unzstd.c" #endif const unsigned long __stack_chk_guard = 0x000a0dff; void __stack_chk_fail(void) { error("stack-protector: Kernel stack is corrupted\n"); } void decompress_kernel(unsigned long boot_heap_start) { unsigned long zimage_start, zimage_size; zimage_start = (unsigned long)(__image_begin); zimage_size = (unsigned long)(__image_end) - (unsigned long)(__image_begin); puts("zimage at: "); puthex(zimage_start); puts(" "); puthex(zimage_size + zimage_start); puts("\n"); /* This area are prepared for mallocing when decompressing */ free_mem_ptr = boot_heap_start; free_mem_end_ptr = boot_heap_start + BOOT_HEAP_SIZE; /* Display standard Linux/MIPS boot prompt */ puts("Uncompressing Linux at load address "); puthex(VMLINUX_LOAD_ADDRESS_ULL); puts("\n"); /* Decompress the kernel with according algorithm */ __decompress((char *)zimage_start, zimage_size, 0, 0, (void *)VMLINUX_LOAD_ADDRESS_ULL, 0, 0, error); if (IS_ENABLED(CONFIG_MIPS_RAW_APPENDED_DTB) && fdt_magic((void *)&__appended_dtb) == FDT_MAGIC) { unsigned int image_size, dtb_size; dtb_size = fdt_totalsize((void *)&__appended_dtb); /* last four bytes is always image size in little endian */ image_size = get_unaligned_le32((void *)__image_end - 4); /* The device tree's address must be properly aligned */ image_size = ALIGN(image_size, STRUCT_ALIGNMENT); puts("Copy device tree to address "); puthex(VMLINUX_LOAD_ADDRESS_ULL + image_size); puts("\n"); /* copy dtb to where the booted kernel will expect it */ memcpy((void *)VMLINUX_LOAD_ADDRESS_ULL + image_size, __appended_dtb, dtb_size); } /* FIXME: should we flush cache here? */ puts("Now, booting the kernel...\n"); }
linux-master
arch/mips/boot/compressed/decompress.c
// SPDX-License-Identifier: GPL-2.0 #include <asm/mach-au1x00/au1000.h> void putc(char c) { alchemy_uart_putchar(AU1000_UART0_PHYS_ADDR, c); }
linux-master
arch/mips/boot/compressed/uart-alchemy.c
// SPDX-License-Identifier: GPL-2.0 /* * arch/mips/boot/compressed/string.c * * Very small subset of simple string routines */ #include <linux/compiler_attributes.h> #include <linux/types.h> void *memcpy(void *dest, const void *src, size_t n) { int i; const char *s = src; char *d = dest; for (i = 0; i < n; i++) d[i] = s[i]; return dest; } void *memset(void *s, int c, size_t n) { int i; char *ss = s; for (i = 0; i < n; i++) ss[i] = c; return s; } void * __weak memmove(void *dest, const void *src, size_t n) { unsigned int i; const char *s = src; char *d = dest; if ((uintptr_t)dest < (uintptr_t)src) { for (i = 0; i < n; i++) d[i] = s[i]; } else { for (i = n; i > 0; i--) d[i - 1] = s[i - 1]; } return dest; }
linux-master
arch/mips/boot/compressed/string.c
// SPDX-License-Identifier: GPL-2.0-only #include "../../ath79/early_printk.c"
linux-master
arch/mips/boot/compressed/uart-ath79.c
// SPDX-License-Identifier: GPL-2.0-only #include "../../lib/bswapdi.c"
linux-master
arch/mips/boot/compressed/bswapdi.c
int main(void) { return 0; }
linux-master
arch/mips/boot/compressed/dummy.c
// SPDX-License-Identifier: GPL-2.0 /* * MIPS-specific debug support for pre-boot environment * * NOTE: putc() is board specific, if your board have a 16550 compatible uart, * please select SYS_SUPPORTS_ZBOOT_UART16550 for your machine. othewise, you * need to implement your own putc(). */ #include <linux/compiler.h> #include <linux/types.h> void __weak putc(char c) { } void puts(const char *s) { char c; while ((c = *s++) != '\0') { putc(c); if (c == '\n') putc('\r'); } } void puthex(unsigned long long val) { unsigned char buf[10]; int i; for (i = 7; i >= 0; i--) { buf[i] = "0123456789ABCDEF"[val & 0x0F]; val >>= 4; } buf[8] = '\0'; puts(buf); }
linux-master
arch/mips/boot/compressed/dbg.c
// SPDX-License-Identifier: GPL-2.0 /* * 16550 compatible uart based serial debug support for zboot */ #include <linux/types.h> #include <linux/serial_reg.h> #include <asm/addrspace.h> #if defined(CONFIG_MACH_LOONGSON64) || defined(CONFIG_MIPS_MALTA) #define UART_BASE 0x1fd003f8 #define PORT(offset) (CKSEG1ADDR(UART_BASE) + (offset)) #endif #ifdef CONFIG_AR7 #include <ar7.h> #define PORT(offset) (CKSEG1ADDR(AR7_REGS_UART0) + (4 * offset)) #endif #ifdef CONFIG_MACH_INGENIC #define INGENIC_UART_BASE_ADDR (0x10030000 + 0x1000 * CONFIG_ZBOOT_INGENIC_UART) #define PORT(offset) (CKSEG1ADDR(INGENIC_UART_BASE_ADDR) + (4 * offset)) #endif #ifndef IOTYPE #define IOTYPE char #endif #ifndef PORT #error please define the serial port address for your own machine #endif static inline unsigned int serial_in(int offset) { return *((volatile IOTYPE *)PORT(offset)) & 0xFF; } static inline void serial_out(int offset, int value) { *((volatile IOTYPE *)PORT(offset)) = value & 0xFF; } void putc(char c) { int timeout = 1000000; while (((serial_in(UART_LSR) & UART_LSR_THRE) == 0) && (timeout-- > 0)) ; serial_out(UART_TX, c); }
linux-master
arch/mips/boot/compressed/uart-16550.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2010 "Wu Zhangjin" <[email protected]> */ #include <sys/types.h> #include <sys/stat.h> #include <errno.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <linux/sizes.h> int main(int argc, char *argv[]) { unsigned long long vmlinux_size, vmlinux_load_addr, vmlinuz_load_addr; struct stat sb; if (argc != 3) { fprintf(stderr, "Usage: %s <pathname> <vmlinux_load_addr>\n", argv[0]); return EXIT_FAILURE; } if (stat(argv[1], &sb) == -1) { perror("stat"); return EXIT_FAILURE; } /* Convert hex characters to dec number */ errno = 0; if (sscanf(argv[2], "%llx", &vmlinux_load_addr) != 1) { if (errno != 0) perror("sscanf"); else fprintf(stderr, "No matching characters\n"); return EXIT_FAILURE; } vmlinux_size = (uint64_t)sb.st_size; vmlinuz_load_addr = vmlinux_load_addr + vmlinux_size; /* * Align with 64KB: KEXEC needs load sections to be aligned to PAGE_SIZE, * which may be as large as 64KB depending on the kernel configuration. */ vmlinuz_load_addr += (SZ_64K - vmlinux_size % SZ_64K); printf("0x%llx\n", vmlinuz_load_addr); return EXIT_SUCCESS; }
linux-master
arch/mips/boot/compressed/calc_vmlinuz_load_addr.c
// SPDX-License-Identifier: GPL-2.0-only #include "../../../../lib/ashldi3.c"
linux-master
arch/mips/boot/compressed/ashldi3.c
// SPDX-License-Identifier: GPL-2.0-only #include "../../../../lib/clz_ctz.c"
linux-master
arch/mips/boot/compressed/clz_ctz.c
// SPDX-License-Identifier: GPL-2.0 #include <asm/setup.h> void putc(char c) { prom_putchar(c); }
linux-master
arch/mips/boot/compressed/uart-prom.c
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 1992 Linus Torvalds * Copyright (C) 1994 - 2000 Ralf Baechle * Copyright (C) 2006 Thomas Bogendoerfer */ #include <linux/delay.h> #include <linux/init.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <linux/kernel.h> #include <asm/i8259.h> #include <asm/io.h> #include <asm/sni.h> #include <asm/irq.h> #include <asm/irq_cpu.h> void (*sni_hwint)(void); asmlinkage void plat_irq_dispatch(void) { sni_hwint(); } /* ISA irq handler */ irqreturn_t sni_isa_irq_handler(int dummy, void *p) { int irq; irq = i8259_irq(); if (unlikely(irq < 0)) return IRQ_NONE; generic_handle_irq(irq); return IRQ_HANDLED; } /* * On systems with i8259-style interrupt controllers we assume for * driver compatibility reasons interrupts 0 - 15 to be the i8295 * interrupts even if the hardware uses a different interrupt numbering. */ void __init arch_init_irq(void) { init_i8259_irqs(); /* Integrated i8259 */ switch (sni_brd_type) { case SNI_BRD_10: case SNI_BRD_10NEW: case SNI_BRD_TOWER_OASIC: case SNI_BRD_MINITOWER: sni_a20r_irq_init(); break; case SNI_BRD_PCI_TOWER: sni_pcit_irq_init(); break; case SNI_BRD_PCI_TOWER_CPLUS: sni_pcit_cplus_irq_init(); break; case SNI_BRD_RM200: sni_rm200_irq_init(); break; case SNI_BRD_PCI_MTOWER: case SNI_BRD_PCI_DESKTOP: case SNI_BRD_PCI_MTOWER_CPLUS: sni_pcimt_irq_init(); break; } }
linux-master
arch/mips/sni/irq.c
/* * PCIMT specific code * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 1996, 97, 98, 2000, 03, 04, 06 Ralf Baechle ([email protected]) * Copyright (C) 2006,2007 Thomas Bogendoerfer ([email protected]) */ #include <linux/init.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <linux/pci.h> #include <linux/serial_8250.h> #include <asm/sni.h> #include <asm/time.h> #include <asm/i8259.h> #include <asm/irq_cpu.h> #define cacheconf (*(volatile unsigned int *)PCIMT_CACHECONF) #define invspace (*(volatile unsigned int *)PCIMT_INVSPACE) static void __init sni_pcimt_sc_init(void) { unsigned int scsiz, sc_size; scsiz = cacheconf & 7; if (scsiz == 0) { printk("Second level cache is deactivated.\n"); return; } if (scsiz >= 6) { printk("Invalid second level cache size configured, " "deactivating second level cache.\n"); cacheconf = 0; return; } sc_size = 128 << scsiz; printk("%dkb second level cache detected, deactivating.\n", sc_size); cacheconf = 0; } /* * A bit more gossip about the iron we're running on ... */ static inline void sni_pcimt_detect(void) { char boardtype[80]; unsigned char csmsr; char *p = boardtype; unsigned int asic; csmsr = *(volatile unsigned char *)PCIMT_CSMSR; p += sprintf(p, "%s PCI", (csmsr & 0x80) ? "RM200" : "RM300"); if ((csmsr & 0x80) == 0) p += sprintf(p, ", board revision %s", (csmsr & 0x20) ? "D" : "C"); asic = csmsr & 0x80; asic = (csmsr & 0x08) ? asic : !asic; p += sprintf(p, ", ASIC PCI Rev %s", asic ? "1.0" : "1.1"); printk("%s.\n", boardtype); } #define PORT(_base,_irq) \ { \ .iobase = _base, \ .irq = _irq, \ .uartclk = 1843200, \ .iotype = UPIO_PORT, \ .flags = UPF_BOOT_AUTOCONF, \ } static struct plat_serial8250_port pcimt_data[] = { PORT(0x3f8, 4), PORT(0x2f8, 3), { }, }; static struct platform_device pcimt_serial8250_device = { .name = "serial8250", .id = PLAT8250_DEV_PLATFORM, .dev = { .platform_data = pcimt_data, }, }; static struct resource pcimt_cmos_rsrc[] = { { .start = 0x70, .end = 0x71, .flags = IORESOURCE_IO }, { .start = 8, .end = 8, .flags = IORESOURCE_IRQ } }; static struct platform_device pcimt_cmos_device = { .name = "rtc_cmos", .num_resources = ARRAY_SIZE(pcimt_cmos_rsrc), .resource = pcimt_cmos_rsrc }; static struct resource sni_io_resource = { .start = 0x00000000UL, .end = 0x03bfffffUL, .name = "PCIMT IO MEM", .flags = IORESOURCE_IO, }; static struct resource pcimt_io_resources[] = { { .start = 0x00, .end = 0x1f, .name = "dma1", .flags = IORESOURCE_BUSY }, { .start = 0x40, .end = 0x5f, .name = "timer", .flags = IORESOURCE_BUSY }, { .start = 0x60, .end = 0x6f, .name = "keyboard", .flags = IORESOURCE_BUSY }, { .start = 0x80, .end = 0x8f, .name = "dma page reg", .flags = IORESOURCE_BUSY }, { .start = 0xc0, .end = 0xdf, .name = "dma2", .flags = IORESOURCE_BUSY }, { .start = 0xcfc, .end = 0xcff, .name = "PCI config data", .flags = IORESOURCE_BUSY } }; static struct resource pcimt_mem_resources[] = { { /* * this region should only be 4 bytes long, * but it's 16MB on all RM300C I've checked */ .start = 0x1a000000, .end = 0x1affffff, .name = "PCI INT ACK", .flags = IORESOURCE_BUSY } }; static struct resource sni_mem_resource = { .start = 0x18000000UL, .end = 0x1fbfffffUL, .name = "PCIMT PCI MEM", .flags = IORESOURCE_MEM }; static void __init sni_pcimt_resource_init(void) { int i; /* request I/O space for devices used on all i[345]86 PCs */ for (i = 0; i < ARRAY_SIZE(pcimt_io_resources); i++) request_resource(&sni_io_resource, pcimt_io_resources + i); /* request MEM space for devices used on all i[345]86 PCs */ for (i = 0; i < ARRAY_SIZE(pcimt_mem_resources); i++) request_resource(&sni_mem_resource, pcimt_mem_resources + i); } extern struct pci_ops sni_pcimt_ops; #ifdef CONFIG_PCI static struct pci_controller sni_controller = { .pci_ops = &sni_pcimt_ops, .mem_resource = &sni_mem_resource, .mem_offset = 0x00000000UL, .io_resource = &sni_io_resource, .io_offset = 0x00000000UL, .io_map_base = SNI_PORT_BASE }; #endif static void enable_pcimt_irq(struct irq_data *d) { unsigned int mask = 1 << (d->irq - PCIMT_IRQ_INT2); *(volatile u8 *) PCIMT_IRQSEL |= mask; } void disable_pcimt_irq(struct irq_data *d) { unsigned int mask = ~(1 << (d->irq - PCIMT_IRQ_INT2)); *(volatile u8 *) PCIMT_IRQSEL &= mask; } static struct irq_chip pcimt_irq_type = { .name = "PCIMT", .irq_mask = disable_pcimt_irq, .irq_unmask = enable_pcimt_irq, }; /* * hwint0 should deal with MP agent, ASIC PCI, EISA NMI and debug * button interrupts. Later ... */ static void pcimt_hwint0(void) { panic("Received int0 but no handler yet ..."); } /* * hwint 1 deals with EISA and SCSI interrupts, * * The EISA_INT bit in CSITPEND is high active, all others are low active. */ static void pcimt_hwint1(void) { u8 pend = *(volatile char *)PCIMT_CSITPEND; unsigned long flags; if (pend & IT_EISA) { int irq; /* * Note: ASIC PCI's builtin interrupt acknowledge feature is * broken. Using it may result in loss of some or all i8259 * interrupts, so don't use PCIMT_INT_ACKNOWLEDGE ... */ irq = i8259_irq(); if (unlikely(irq < 0)) return; do_IRQ(irq); } if (!(pend & IT_SCSI)) { flags = read_c0_status(); clear_c0_status(ST0_IM); do_IRQ(PCIMT_IRQ_SCSI); write_c0_status(flags); } } /* * hwint 3 should deal with the PCI A - D interrupts, */ static void pcimt_hwint3(void) { u8 pend = *(volatile char *)PCIMT_CSITPEND; int irq; pend &= (IT_INTA | IT_INTB | IT_INTC | IT_INTD); pend ^= (IT_INTA | IT_INTB | IT_INTC | IT_INTD); clear_c0_status(IE_IRQ3); irq = PCIMT_IRQ_INT2 + ffs(pend) - 1; do_IRQ(irq); set_c0_status(IE_IRQ3); } static void sni_pcimt_hwint(void) { u32 pending = read_c0_cause() & read_c0_status(); if (pending & C_IRQ5) do_IRQ(MIPS_CPU_IRQ_BASE + 7); else if (pending & C_IRQ4) do_IRQ(MIPS_CPU_IRQ_BASE + 6); else if (pending & C_IRQ3) pcimt_hwint3(); else if (pending & C_IRQ1) pcimt_hwint1(); else if (pending & C_IRQ0) { pcimt_hwint0(); } } void __init sni_pcimt_irq_init(void) { int i; *(volatile u8 *) PCIMT_IRQSEL = IT_ETH | IT_EISA; mips_cpu_irq_init(); /* Actually we've got more interrupts to handle ... */ for (i = PCIMT_IRQ_INT2; i <= PCIMT_IRQ_SCSI; i++) irq_set_chip_and_handler(i, &pcimt_irq_type, handle_level_irq); sni_hwint = sni_pcimt_hwint; change_c0_status(ST0_IM, IE_IRQ1|IE_IRQ3); } void __init sni_pcimt_init(void) { sni_pcimt_detect(); sni_pcimt_sc_init(); ioport_resource.end = sni_io_resource.end; #ifdef CONFIG_PCI PCIBIOS_MIN_IO = 0x9000; register_pci_controller(&sni_controller); #endif sni_pcimt_resource_init(); } static int __init snirm_pcimt_setup_devinit(void) { switch (sni_brd_type) { case SNI_BRD_PCI_MTOWER: case SNI_BRD_PCI_DESKTOP: case SNI_BRD_PCI_MTOWER_CPLUS: platform_device_register(&pcimt_serial8250_device); platform_device_register(&pcimt_cmos_device); break; } return 0; } device_initcall(snirm_pcimt_setup_devinit);
linux-master
arch/mips/sni/pcimt.c
// SPDX-License-Identifier: GPL-2.0 /* * linux/arch/mips/sni/process.c * * Reset a SNI machine. */ #include <linux/delay.h> #include <asm/io.h> #include <asm/reboot.h> #include <asm/sni.h> /* * This routine reboots the machine by asking the keyboard * controller to pulse the reset-line low. We try that for a while, * and if it doesn't work, we do some other stupid things. */ static inline void kb_wait(void) { int i; for (i = 0; i < 0x10000; i++) if ((inb_p(0x64) & 0x02) == 0) break; } /* XXX This ends up at the ARC firmware prompt ... */ void sni_machine_restart(char *command) { int i; /* This does a normal via the keyboard controller like a PC. We can do that easier ... */ local_irq_disable(); for (;;) { for (i = 0; i < 100; i++) { kb_wait(); udelay(50); outb_p(0xfe, 0x64); /* pulse reset low */ udelay(50); } } } void sni_machine_power_off(void) { *(volatile unsigned char *)PCIMT_CSWCSM = 0xfd; }
linux-master
arch/mips/sni/reset.c
/* * Setup pointers to hardware-dependent routines. * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 1996, 97, 98, 2000, 03, 04, 06 Ralf Baechle ([email protected]) * Copyright (C) 2006,2007 Thomas Bogendoerfer ([email protected]) */ #include <linux/eisa.h> #include <linux/init.h> #include <linux/export.h> #include <linux/console.h> #include <linux/fb.h> #include <linux/screen_info.h> #ifdef CONFIG_FW_ARC #include <asm/fw/arc/types.h> #include <asm/sgialib.h> #endif #ifdef CONFIG_FW_SNIPROM #include <asm/mipsprom.h> #endif #include <asm/bootinfo.h> #include <asm/cpu.h> #include <asm/io.h> #include <asm/reboot.h> #include <asm/sni.h> unsigned int sni_brd_type; EXPORT_SYMBOL(sni_brd_type); extern void sni_machine_restart(char *command); extern void sni_machine_power_off(void); static void __init sni_display_setup(void) { #if defined(CONFIG_VT) && defined(CONFIG_VGA_CONSOLE) && defined(CONFIG_FW_ARC) struct screen_info *si = &screen_info; DISPLAY_STATUS *di; di = ArcGetDisplayStatus(1); if (di) { si->orig_x = di->CursorXPosition; si->orig_y = di->CursorYPosition; si->orig_video_cols = di->CursorMaxXPosition; si->orig_video_lines = di->CursorMaxYPosition; si->orig_video_isVGA = VIDEO_TYPE_VGAC; si->orig_video_points = 16; } #endif } static void __init sni_console_setup(void) { #ifndef CONFIG_FW_ARC char *ctype; char *cdev; char *baud; int port; static char options[8] __initdata; cdev = prom_getenv("console_dev"); if (strncmp(cdev, "tty", 3) == 0) { ctype = prom_getenv("console"); switch (*ctype) { default: case 'l': port = 0; baud = prom_getenv("lbaud"); break; case 'r': port = 1; baud = prom_getenv("rbaud"); break; } if (baud) strcpy(options, baud); if (strncmp(cdev, "tty552", 6) == 0) add_preferred_console("ttyS", port, baud ? options : NULL); else add_preferred_console("ttySC", port, baud ? options : NULL); } #endif } #ifdef DEBUG static void __init sni_idprom_dump(void) { int i; pr_debug("SNI IDProm dump:\n"); for (i = 0; i < 256; i++) { if (i%16 == 0) pr_debug("%04x ", i); printk("%02x ", *(unsigned char *) (SNI_IDPROM_BASE + i)); if (i % 16 == 15) printk("\n"); } } #endif void __init plat_mem_setup(void) { int cputype; set_io_port_base(SNI_PORT_BASE); // ioport_resource.end = sni_io_resource.end; /* * Setup (E)ISA I/O memory access stuff */ #ifdef CONFIG_EISA EISA_bus = 1; #endif sni_brd_type = *(unsigned char *)SNI_IDPROM_BRDTYPE; cputype = *(unsigned char *)SNI_IDPROM_CPUTYPE; switch (sni_brd_type) { case SNI_BRD_TOWER_OASIC: switch (cputype) { case SNI_CPU_M8030: system_type = "RM400-330"; break; case SNI_CPU_M8031: system_type = "RM400-430"; break; case SNI_CPU_M8037: system_type = "RM400-530"; break; case SNI_CPU_M8034: system_type = "RM400-730"; break; default: system_type = "RM400-xxx"; break; } break; case SNI_BRD_MINITOWER: switch (cputype) { case SNI_CPU_M8021: case SNI_CPU_M8043: system_type = "RM400-120"; break; case SNI_CPU_M8040: system_type = "RM400-220"; break; case SNI_CPU_M8053: system_type = "RM400-225"; break; case SNI_CPU_M8050: system_type = "RM400-420"; break; default: system_type = "RM400-xxx"; break; } break; case SNI_BRD_PCI_TOWER: system_type = "RM400-Cxx"; break; case SNI_BRD_RM200: system_type = "RM200-xxx"; break; case SNI_BRD_PCI_MTOWER: system_type = "RM300-Cxx"; break; case SNI_BRD_PCI_DESKTOP: switch (read_c0_prid() & PRID_IMP_MASK) { case PRID_IMP_R4600: case PRID_IMP_R4700: system_type = "RM200-C20"; break; case PRID_IMP_R5000: system_type = "RM200-C40"; break; default: system_type = "RM200-Cxx"; break; } break; case SNI_BRD_PCI_TOWER_CPLUS: system_type = "RM400-Exx"; break; case SNI_BRD_PCI_MTOWER_CPLUS: system_type = "RM300-Exx"; break; } pr_debug("Found SNI brdtype %02x name %s\n", sni_brd_type, system_type); #ifdef DEBUG sni_idprom_dump(); #endif switch (sni_brd_type) { case SNI_BRD_10: case SNI_BRD_10NEW: case SNI_BRD_TOWER_OASIC: case SNI_BRD_MINITOWER: sni_a20r_init(); break; case SNI_BRD_PCI_TOWER: case SNI_BRD_PCI_TOWER_CPLUS: sni_pcit_init(); break; case SNI_BRD_RM200: sni_rm200_init(); break; case SNI_BRD_PCI_MTOWER: case SNI_BRD_PCI_DESKTOP: case SNI_BRD_PCI_MTOWER_CPLUS: sni_pcimt_init(); break; } _machine_restart = sni_machine_restart; pm_power_off = sni_machine_power_off; sni_display_setup(); sni_console_setup(); } #ifdef CONFIG_PCI #include <linux/pci.h> #include <video/vga.h> #include <video/cirrus.h> static void quirk_cirrus_ram_size(struct pci_dev *dev) { u16 cmd; /* * firmware doesn't set the ram size correct, so we * need to do it here, otherwise we get screen corruption * on older Cirrus chips */ pci_read_config_word(dev, PCI_COMMAND, &cmd); if ((cmd & (PCI_COMMAND_IO|PCI_COMMAND_MEMORY)) == (PCI_COMMAND_IO|PCI_COMMAND_MEMORY)) { vga_wseq(NULL, CL_SEQR6, 0x12); /* unlock all extension registers */ vga_wseq(NULL, CL_SEQRF, 0x18); } } DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5434_8, quirk_cirrus_ram_size); DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5436, quirk_cirrus_ram_size); DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, quirk_cirrus_ram_size); #endif
linux-master
arch/mips/sni/setup.c
// SPDX-License-Identifier: GPL-2.0 #include <linux/types.h> #include <linux/i8253.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <linux/smp.h> #include <linux/time.h> #include <linux/clockchips.h> #include <asm/sni.h> #include <asm/time.h> #define SNI_CLOCK_TICK_RATE 3686400 #define SNI_COUNTER2_DIV 64 #define SNI_COUNTER0_DIV ((SNI_CLOCK_TICK_RATE / SNI_COUNTER2_DIV) / HZ) static int a20r_set_periodic(struct clock_event_device *evt) { *(volatile u8 *)(A20R_PT_CLOCK_BASE + 12) = 0x34; wmb(); *(volatile u8 *)(A20R_PT_CLOCK_BASE + 0) = SNI_COUNTER0_DIV & 0xff; wmb(); *(volatile u8 *)(A20R_PT_CLOCK_BASE + 0) = SNI_COUNTER0_DIV >> 8; wmb(); *(volatile u8 *)(A20R_PT_CLOCK_BASE + 12) = 0xb4; wmb(); *(volatile u8 *)(A20R_PT_CLOCK_BASE + 8) = SNI_COUNTER2_DIV & 0xff; wmb(); *(volatile u8 *)(A20R_PT_CLOCK_BASE + 8) = SNI_COUNTER2_DIV >> 8; wmb(); return 0; } static struct clock_event_device a20r_clockevent_device = { .name = "a20r-timer", .features = CLOCK_EVT_FEAT_PERIODIC, /* .mult, .shift, .max_delta_ns and .min_delta_ns left uninitialized */ .rating = 300, .irq = SNI_A20R_IRQ_TIMER, .set_state_periodic = a20r_set_periodic, }; static irqreturn_t a20r_interrupt(int irq, void *dev_id) { struct clock_event_device *cd = dev_id; *(volatile u8 *)A20R_PT_TIM0_ACK = 0; wmb(); cd->event_handler(cd); return IRQ_HANDLED; } /* * a20r platform uses 2 counters to divide the input frequency. * Counter 2 output is connected to Counter 0 & 1 input. */ static void __init sni_a20r_timer_setup(void) { struct clock_event_device *cd = &a20r_clockevent_device; unsigned int cpu = smp_processor_id(); cd->cpumask = cpumask_of(cpu); clockevents_register_device(cd); if (request_irq(SNI_A20R_IRQ_TIMER, a20r_interrupt, IRQF_PERCPU | IRQF_TIMER, "a20r-timer", cd)) pr_err("Failed to register a20r-timer interrupt\n"); } #define SNI_8254_TICK_RATE 1193182UL #define SNI_8254_TCSAMP_COUNTER ((SNI_8254_TICK_RATE / HZ) + 255) static __init unsigned long dosample(void) { u32 ct0, ct1; volatile u8 msb; /* Start the counter. */ outb_p(0x34, 0x43); outb_p(SNI_8254_TCSAMP_COUNTER & 0xff, 0x40); outb(SNI_8254_TCSAMP_COUNTER >> 8, 0x40); /* Get initial counter invariant */ ct0 = read_c0_count(); /* Latch and spin until top byte of counter0 is zero */ do { outb(0x00, 0x43); (void) inb(0x40); msb = inb(0x40); ct1 = read_c0_count(); } while (msb); /* Stop the counter. */ outb(0x38, 0x43); /* * Return the difference, this is how far the r4k counter increments * for every 1/HZ seconds. We round off the nearest 1 MHz of master * clock (= 1000000 / HZ / 2). */ /*return (ct1 - ct0 + (500000/HZ/2)) / (500000/HZ) * (500000/HZ);*/ return (ct1 - ct0) / (500000/HZ) * (500000/HZ); } /* * Here we need to calibrate the cycle counter to at least be close. */ void __init plat_time_init(void) { unsigned long r4k_ticks[3]; unsigned long r4k_tick; /* * Figure out the r4k offset, the algorithm is very simple and works in * _all_ cases as long as the 8254 counter register itself works ok (as * an interrupt driving timer it does not because of bug, this is why * we are using the onchip r4k counter/compare register to serve this * purpose, but for r4k_offset calculation it will work ok for us). * There are other very complicated ways of performing this calculation * but this one works just fine so I am not going to futz around. ;-) */ printk(KERN_INFO "Calibrating system timer... "); dosample(); /* Prime cache. */ dosample(); /* Prime cache. */ /* Zero is NOT an option. */ do { r4k_ticks[0] = dosample(); } while (!r4k_ticks[0]); do { r4k_ticks[1] = dosample(); } while (!r4k_ticks[1]); if (r4k_ticks[0] != r4k_ticks[1]) { printk("warning: timer counts differ, retrying... "); r4k_ticks[2] = dosample(); if (r4k_ticks[2] == r4k_ticks[0] || r4k_ticks[2] == r4k_ticks[1]) r4k_tick = r4k_ticks[2]; else { printk("disagreement, using average... "); r4k_tick = (r4k_ticks[0] + r4k_ticks[1] + r4k_ticks[2]) / 3; } } else r4k_tick = r4k_ticks[0]; printk("%d [%d.%04d MHz CPU]\n", (int) r4k_tick, (int) (r4k_tick / (500000 / HZ)), (int) (r4k_tick % (500000 / HZ))); mips_hpt_frequency = r4k_tick * HZ; switch (sni_brd_type) { case SNI_BRD_10: case SNI_BRD_10NEW: case SNI_BRD_TOWER_OASIC: case SNI_BRD_MINITOWER: sni_a20r_timer_setup(); break; } setup_pit_timer(); }
linux-master
arch/mips/sni/time.c
/* * PCI Tower specific code * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 2006 Thomas Bogendoerfer ([email protected]) */ #include <linux/init.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <linux/pci.h> #include <linux/serial_8250.h> #include <asm/sni.h> #include <asm/time.h> #include <asm/irq_cpu.h> #define PORT(_base,_irq) \ { \ .iobase = _base, \ .irq = _irq, \ .uartclk = 1843200, \ .iotype = UPIO_PORT, \ .flags = UPF_BOOT_AUTOCONF, \ } static struct plat_serial8250_port pcit_data[] = { PORT(0x3f8, 0), PORT(0x2f8, 3), { }, }; static struct platform_device pcit_serial8250_device = { .name = "serial8250", .id = PLAT8250_DEV_PLATFORM, .dev = { .platform_data = pcit_data, }, }; static struct plat_serial8250_port pcit_cplus_data[] = { PORT(0x3f8, 0), PORT(0x2f8, 3), PORT(0x3e8, 4), PORT(0x2e8, 3), { }, }; static struct platform_device pcit_cplus_serial8250_device = { .name = "serial8250", .id = PLAT8250_DEV_PLATFORM, .dev = { .platform_data = pcit_cplus_data, }, }; static struct resource pcit_cmos_rsrc[] = { { .start = 0x70, .end = 0x71, .flags = IORESOURCE_IO }, { .start = 8, .end = 8, .flags = IORESOURCE_IRQ } }; static struct platform_device pcit_cmos_device = { .name = "rtc_cmos", .num_resources = ARRAY_SIZE(pcit_cmos_rsrc), .resource = pcit_cmos_rsrc }; static struct platform_device pcit_pcspeaker_pdev = { .name = "pcspkr", .id = -1, }; static struct resource sni_io_resource = { .start = 0x00000000UL, .end = 0x03bfffffUL, .name = "PCIT IO", .flags = IORESOURCE_IO, }; static struct resource pcit_io_resources[] = { { .start = 0x00, .end = 0x1f, .name = "dma1", .flags = IORESOURCE_BUSY }, { .start = 0x40, .end = 0x5f, .name = "timer", .flags = IORESOURCE_BUSY }, { .start = 0x60, .end = 0x6f, .name = "keyboard", .flags = IORESOURCE_BUSY }, { .start = 0x80, .end = 0x8f, .name = "dma page reg", .flags = IORESOURCE_BUSY }, { .start = 0xc0, .end = 0xdf, .name = "dma2", .flags = IORESOURCE_BUSY }, { .start = 0xcf8, .end = 0xcfb, .name = "PCI config addr", .flags = IORESOURCE_BUSY }, { .start = 0xcfc, .end = 0xcff, .name = "PCI config data", .flags = IORESOURCE_BUSY } }; static void __init sni_pcit_resource_init(void) { int i; /* request I/O space for devices used on all i[345]86 PCs */ for (i = 0; i < ARRAY_SIZE(pcit_io_resources); i++) request_resource(&sni_io_resource, pcit_io_resources + i); } extern struct pci_ops sni_pcit_ops; #ifdef CONFIG_PCI static struct resource sni_mem_resource = { .start = 0x18000000UL, .end = 0x1fbfffffUL, .name = "PCIT PCI MEM", .flags = IORESOURCE_MEM }; static struct pci_controller sni_pcit_controller = { .pci_ops = &sni_pcit_ops, .mem_resource = &sni_mem_resource, .mem_offset = 0x00000000UL, .io_resource = &sni_io_resource, .io_offset = 0x00000000UL, .io_map_base = SNI_PORT_BASE }; #endif /* CONFIG_PCI */ static void enable_pcit_irq(struct irq_data *d) { u32 mask = 1 << (d->irq - SNI_PCIT_INT_START + 24); *(volatile u32 *)SNI_PCIT_INT_REG |= mask; } void disable_pcit_irq(struct irq_data *d) { u32 mask = 1 << (d->irq - SNI_PCIT_INT_START + 24); *(volatile u32 *)SNI_PCIT_INT_REG &= ~mask; } static struct irq_chip pcit_irq_type = { .name = "PCIT", .irq_mask = disable_pcit_irq, .irq_unmask = enable_pcit_irq, }; static void pcit_hwint1(void) { u32 pending = *(volatile u32 *)SNI_PCIT_INT_REG; int irq; clear_c0_status(IE_IRQ1); irq = ffs((pending >> 16) & 0x7f); if (likely(irq > 0)) do_IRQ(irq + SNI_PCIT_INT_START - 1); set_c0_status(IE_IRQ1); } static void pcit_hwint0(void) { u32 pending = *(volatile u32 *)SNI_PCIT_INT_REG; int irq; clear_c0_status(IE_IRQ0); irq = ffs((pending >> 16) & 0x3f); if (likely(irq > 0)) do_IRQ(irq + SNI_PCIT_INT_START - 1); set_c0_status(IE_IRQ0); } static void sni_pcit_hwint(void) { u32 pending = read_c0_cause() & read_c0_status(); if (pending & C_IRQ1) pcit_hwint1(); else if (pending & C_IRQ2) do_IRQ(MIPS_CPU_IRQ_BASE + 4); else if (pending & C_IRQ3) do_IRQ(MIPS_CPU_IRQ_BASE + 5); else if (pending & C_IRQ5) do_IRQ(MIPS_CPU_IRQ_BASE + 7); } static void sni_pcit_hwint_cplus(void) { u32 pending = read_c0_cause() & read_c0_status(); if (pending & C_IRQ0) pcit_hwint0(); else if (pending & C_IRQ1) do_IRQ(MIPS_CPU_IRQ_BASE + 3); else if (pending & C_IRQ2) do_IRQ(MIPS_CPU_IRQ_BASE + 4); else if (pending & C_IRQ3) do_IRQ(MIPS_CPU_IRQ_BASE + 5); else if (pending & C_IRQ5) do_IRQ(MIPS_CPU_IRQ_BASE + 7); } void __init sni_pcit_irq_init(void) { int i; mips_cpu_irq_init(); for (i = SNI_PCIT_INT_START; i <= SNI_PCIT_INT_END; i++) irq_set_chip_and_handler(i, &pcit_irq_type, handle_level_irq); *(volatile u32 *)SNI_PCIT_INT_REG = 0; sni_hwint = sni_pcit_hwint; change_c0_status(ST0_IM, IE_IRQ1); if (request_irq(SNI_PCIT_INT_START + 6, sni_isa_irq_handler, 0, "ISA", NULL)) pr_err("Failed to register ISA interrupt\n"); } void __init sni_pcit_cplus_irq_init(void) { int i; mips_cpu_irq_init(); for (i = SNI_PCIT_INT_START; i <= SNI_PCIT_INT_END; i++) irq_set_chip_and_handler(i, &pcit_irq_type, handle_level_irq); *(volatile u32 *)SNI_PCIT_INT_REG = 0x40000000; sni_hwint = sni_pcit_hwint_cplus; change_c0_status(ST0_IM, IE_IRQ0); if (request_irq(MIPS_CPU_IRQ_BASE + 3, sni_isa_irq_handler, 0, "ISA", NULL)) pr_err("Failed to register ISA interrupt\n"); } void __init sni_pcit_init(void) { ioport_resource.end = sni_io_resource.end; #ifdef CONFIG_PCI PCIBIOS_MIN_IO = 0x9000; register_pci_controller(&sni_pcit_controller); #endif sni_pcit_resource_init(); } static int __init snirm_pcit_setup_devinit(void) { switch (sni_brd_type) { case SNI_BRD_PCI_TOWER: platform_device_register(&pcit_serial8250_device); platform_device_register(&pcit_cmos_device); platform_device_register(&pcit_pcspeaker_pdev); break; case SNI_BRD_PCI_TOWER_CPLUS: platform_device_register(&pcit_cplus_serial8250_device); platform_device_register(&pcit_cmos_device); platform_device_register(&pcit_pcspeaker_pdev); break; } return 0; } device_initcall(snirm_pcit_setup_devinit);
linux-master
arch/mips/sni/pcit.c
/* * RM200 specific code * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 2006,2007 Thomas Bogendoerfer ([email protected]) * * i8259 parts ripped out of arch/mips/kernel/i8259.c */ #include <linux/delay.h> #include <linux/init.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <linux/platform_device.h> #include <linux/serial_8250.h> #include <linux/io.h> #include <asm/sni.h> #include <asm/time.h> #include <asm/irq_cpu.h> #define RM200_I8259A_IRQ_BASE 32 #define MEMPORT(_base,_irq) \ { \ .mapbase = _base, \ .irq = _irq, \ .uartclk = 1843200, \ .iotype = UPIO_MEM, \ .flags = UPF_BOOT_AUTOCONF|UPF_IOREMAP, \ } static struct plat_serial8250_port rm200_data[] = { MEMPORT(0x160003f8, RM200_I8259A_IRQ_BASE + 4), MEMPORT(0x160002f8, RM200_I8259A_IRQ_BASE + 3), { }, }; static struct platform_device rm200_serial8250_device = { .name = "serial8250", .id = PLAT8250_DEV_PLATFORM, .dev = { .platform_data = rm200_data, }, }; static struct resource rm200_ds1216_rsrc[] = { { .start = 0x1cd41ffc, .end = 0x1cd41fff, .flags = IORESOURCE_MEM } }; static struct platform_device rm200_ds1216_device = { .name = "rtc-ds1216", .num_resources = ARRAY_SIZE(rm200_ds1216_rsrc), .resource = rm200_ds1216_rsrc }; static struct resource snirm_82596_rm200_rsrc[] = { { .start = 0x18000000, .end = 0x180fffff, .flags = IORESOURCE_MEM }, { .start = 0x1b000000, .end = 0x1b000004, .flags = IORESOURCE_MEM }, { .start = 0x1ff00000, .end = 0x1ff00020, .flags = IORESOURCE_MEM }, { .start = 27, .end = 27, .flags = IORESOURCE_IRQ }, { .flags = 0x00 } }; static struct platform_device snirm_82596_rm200_pdev = { .name = "snirm_82596", .num_resources = ARRAY_SIZE(snirm_82596_rm200_rsrc), .resource = snirm_82596_rm200_rsrc }; static struct resource snirm_53c710_rm200_rsrc[] = { { .start = 0x19000000, .end = 0x190fffff, .flags = IORESOURCE_MEM }, { .start = 26, .end = 26, .flags = IORESOURCE_IRQ } }; static struct platform_device snirm_53c710_rm200_pdev = { .name = "snirm_53c710", .num_resources = ARRAY_SIZE(snirm_53c710_rm200_rsrc), .resource = snirm_53c710_rm200_rsrc }; static int __init snirm_setup_devinit(void) { if (sni_brd_type == SNI_BRD_RM200) { platform_device_register(&rm200_serial8250_device); platform_device_register(&rm200_ds1216_device); platform_device_register(&snirm_82596_rm200_pdev); platform_device_register(&snirm_53c710_rm200_pdev); sni_eisa_root_init(); } return 0; } device_initcall(snirm_setup_devinit); /* * RM200 has an ISA and an EISA bus. The iSA bus is only used * for onboard devices and also has twi i8259 PICs. Since these * PICs are no accessible via inb/outb the following code uses * readb/writeb to access them */ static DEFINE_RAW_SPINLOCK(sni_rm200_i8259A_lock); #define PIC_CMD 0x00 #define PIC_IMR 0x01 #define PIC_ISR PIC_CMD #define PIC_POLL PIC_ISR #define PIC_OCW3 PIC_ISR /* i8259A PIC related value */ #define PIC_CASCADE_IR 2 #define MASTER_ICW4_DEFAULT 0x01 #define SLAVE_ICW4_DEFAULT 0x01 /* * This contains the irq mask for both 8259A irq controllers, */ static unsigned int rm200_cached_irq_mask = 0xffff; static __iomem u8 *rm200_pic_master; static __iomem u8 *rm200_pic_slave; #define cached_master_mask (rm200_cached_irq_mask) #define cached_slave_mask (rm200_cached_irq_mask >> 8) static void sni_rm200_disable_8259A_irq(struct irq_data *d) { unsigned int mask, irq = d->irq - RM200_I8259A_IRQ_BASE; unsigned long flags; mask = 1 << irq; raw_spin_lock_irqsave(&sni_rm200_i8259A_lock, flags); rm200_cached_irq_mask |= mask; if (irq & 8) writeb(cached_slave_mask, rm200_pic_slave + PIC_IMR); else writeb(cached_master_mask, rm200_pic_master + PIC_IMR); raw_spin_unlock_irqrestore(&sni_rm200_i8259A_lock, flags); } static void sni_rm200_enable_8259A_irq(struct irq_data *d) { unsigned int mask, irq = d->irq - RM200_I8259A_IRQ_BASE; unsigned long flags; mask = ~(1 << irq); raw_spin_lock_irqsave(&sni_rm200_i8259A_lock, flags); rm200_cached_irq_mask &= mask; if (irq & 8) writeb(cached_slave_mask, rm200_pic_slave + PIC_IMR); else writeb(cached_master_mask, rm200_pic_master + PIC_IMR); raw_spin_unlock_irqrestore(&sni_rm200_i8259A_lock, flags); } static inline int sni_rm200_i8259A_irq_real(unsigned int irq) { int value; int irqmask = 1 << irq; if (irq < 8) { writeb(0x0B, rm200_pic_master + PIC_CMD); value = readb(rm200_pic_master + PIC_CMD) & irqmask; writeb(0x0A, rm200_pic_master + PIC_CMD); return value; } writeb(0x0B, rm200_pic_slave + PIC_CMD); /* ISR register */ value = readb(rm200_pic_slave + PIC_CMD) & (irqmask >> 8); writeb(0x0A, rm200_pic_slave + PIC_CMD); return value; } /* * Careful! The 8259A is a fragile beast, it pretty * much _has_ to be done exactly like this (mask it * first, _then_ send the EOI, and the order of EOI * to the two 8259s is important! */ void sni_rm200_mask_and_ack_8259A(struct irq_data *d) { unsigned int irqmask, irq = d->irq - RM200_I8259A_IRQ_BASE; unsigned long flags; irqmask = 1 << irq; raw_spin_lock_irqsave(&sni_rm200_i8259A_lock, flags); /* * Lightweight spurious IRQ detection. We do not want * to overdo spurious IRQ handling - it's usually a sign * of hardware problems, so we only do the checks we can * do without slowing down good hardware unnecessarily. * * Note that IRQ7 and IRQ15 (the two spurious IRQs * usually resulting from the 8259A-1|2 PICs) occur * even if the IRQ is masked in the 8259A. Thus we * can check spurious 8259A IRQs without doing the * quite slow i8259A_irq_real() call for every IRQ. * This does not cover 100% of spurious interrupts, * but should be enough to warn the user that there * is something bad going on ... */ if (rm200_cached_irq_mask & irqmask) goto spurious_8259A_irq; rm200_cached_irq_mask |= irqmask; handle_real_irq: if (irq & 8) { readb(rm200_pic_slave + PIC_IMR); writeb(cached_slave_mask, rm200_pic_slave + PIC_IMR); writeb(0x60+(irq & 7), rm200_pic_slave + PIC_CMD); writeb(0x60+PIC_CASCADE_IR, rm200_pic_master + PIC_CMD); } else { readb(rm200_pic_master + PIC_IMR); writeb(cached_master_mask, rm200_pic_master + PIC_IMR); writeb(0x60+irq, rm200_pic_master + PIC_CMD); } raw_spin_unlock_irqrestore(&sni_rm200_i8259A_lock, flags); return; spurious_8259A_irq: /* * this is the slow path - should happen rarely. */ if (sni_rm200_i8259A_irq_real(irq)) /* * oops, the IRQ _is_ in service according to the * 8259A - not spurious, go handle it. */ goto handle_real_irq; { static int spurious_irq_mask; /* * At this point we can be sure the IRQ is spurious, * let's ACK and report it. [once per IRQ] */ if (!(spurious_irq_mask & irqmask)) { printk(KERN_DEBUG "spurious RM200 8259A interrupt: IRQ%d.\n", irq); spurious_irq_mask |= irqmask; } atomic_inc(&irq_err_count); /* * Theoretically we do not have to handle this IRQ, * but in Linux this does not cause problems and is * simpler for us. */ goto handle_real_irq; } } static struct irq_chip sni_rm200_i8259A_chip = { .name = "RM200-XT-PIC", .irq_mask = sni_rm200_disable_8259A_irq, .irq_unmask = sni_rm200_enable_8259A_irq, .irq_mask_ack = sni_rm200_mask_and_ack_8259A, }; /* * Do the traditional i8259 interrupt polling thing. This is for the few * cases where no better interrupt acknowledge method is available and we * absolutely must touch the i8259. */ static inline int sni_rm200_i8259_irq(void) { int irq; raw_spin_lock(&sni_rm200_i8259A_lock); /* Perform an interrupt acknowledge cycle on controller 1. */ writeb(0x0C, rm200_pic_master + PIC_CMD); /* prepare for poll */ irq = readb(rm200_pic_master + PIC_CMD) & 7; if (irq == PIC_CASCADE_IR) { /* * Interrupt is cascaded so perform interrupt * acknowledge on controller 2. */ writeb(0x0C, rm200_pic_slave + PIC_CMD); /* prepare for poll */ irq = (readb(rm200_pic_slave + PIC_CMD) & 7) + 8; } if (unlikely(irq == 7)) { /* * This may be a spurious interrupt. * * Read the interrupt status register (ISR). If the most * significant bit is not set then there is no valid * interrupt. */ writeb(0x0B, rm200_pic_master + PIC_ISR); /* ISR register */ if (~readb(rm200_pic_master + PIC_ISR) & 0x80) irq = -1; } raw_spin_unlock(&sni_rm200_i8259A_lock); return likely(irq >= 0) ? irq + RM200_I8259A_IRQ_BASE : irq; } void sni_rm200_init_8259A(void) { unsigned long flags; raw_spin_lock_irqsave(&sni_rm200_i8259A_lock, flags); writeb(0xff, rm200_pic_master + PIC_IMR); writeb(0xff, rm200_pic_slave + PIC_IMR); writeb(0x11, rm200_pic_master + PIC_CMD); writeb(0, rm200_pic_master + PIC_IMR); writeb(1U << PIC_CASCADE_IR, rm200_pic_master + PIC_IMR); writeb(MASTER_ICW4_DEFAULT, rm200_pic_master + PIC_IMR); writeb(0x11, rm200_pic_slave + PIC_CMD); writeb(8, rm200_pic_slave + PIC_IMR); writeb(PIC_CASCADE_IR, rm200_pic_slave + PIC_IMR); writeb(SLAVE_ICW4_DEFAULT, rm200_pic_slave + PIC_IMR); udelay(100); /* wait for 8259A to initialize */ writeb(cached_master_mask, rm200_pic_master + PIC_IMR); writeb(cached_slave_mask, rm200_pic_slave + PIC_IMR); raw_spin_unlock_irqrestore(&sni_rm200_i8259A_lock, flags); } /* * IRQ2 is cascade interrupt to second interrupt controller */ static struct resource sni_rm200_pic1_resource = { .name = "onboard ISA pic1", .start = 0x16000020, .end = 0x16000023, .flags = IORESOURCE_BUSY }; static struct resource sni_rm200_pic2_resource = { .name = "onboard ISA pic2", .start = 0x160000a0, .end = 0x160000a3, .flags = IORESOURCE_BUSY }; /* ISA irq handler */ static irqreturn_t sni_rm200_i8259A_irq_handler(int dummy, void *p) { int irq; irq = sni_rm200_i8259_irq(); if (unlikely(irq < 0)) return IRQ_NONE; do_IRQ(irq); return IRQ_HANDLED; } void __init sni_rm200_i8259_irqs(void) { int i; rm200_pic_master = ioremap(0x16000020, 4); if (!rm200_pic_master) return; rm200_pic_slave = ioremap(0x160000a0, 4); if (!rm200_pic_slave) { iounmap(rm200_pic_master); return; } insert_resource(&iomem_resource, &sni_rm200_pic1_resource); insert_resource(&iomem_resource, &sni_rm200_pic2_resource); sni_rm200_init_8259A(); for (i = RM200_I8259A_IRQ_BASE; i < RM200_I8259A_IRQ_BASE + 16; i++) irq_set_chip_and_handler(i, &sni_rm200_i8259A_chip, handle_level_irq); if (request_irq(RM200_I8259A_IRQ_BASE + PIC_CASCADE_IR, no_action, IRQF_NO_THREAD, "cascade", NULL)) pr_err("Failed to register cascade interrupt\n"); } #define SNI_RM200_INT_STAT_REG CKSEG1ADDR(0xbc000000) #define SNI_RM200_INT_ENA_REG CKSEG1ADDR(0xbc080000) #define SNI_RM200_INT_START 24 #define SNI_RM200_INT_END 28 static void enable_rm200_irq(struct irq_data *d) { unsigned int mask = 1 << (d->irq - SNI_RM200_INT_START); *(volatile u8 *)SNI_RM200_INT_ENA_REG &= ~mask; } void disable_rm200_irq(struct irq_data *d) { unsigned int mask = 1 << (d->irq - SNI_RM200_INT_START); *(volatile u8 *)SNI_RM200_INT_ENA_REG |= mask; } static struct irq_chip rm200_irq_type = { .name = "RM200", .irq_mask = disable_rm200_irq, .irq_unmask = enable_rm200_irq, }; static void sni_rm200_hwint(void) { u32 pending = read_c0_cause() & read_c0_status(); u8 mask; u8 stat; int irq; if (pending & C_IRQ5) do_IRQ(MIPS_CPU_IRQ_BASE + 7); else if (pending & C_IRQ0) { clear_c0_status(IE_IRQ0); mask = *(volatile u8 *)SNI_RM200_INT_ENA_REG ^ 0x1f; stat = *(volatile u8 *)SNI_RM200_INT_STAT_REG ^ 0x14; irq = ffs(stat & mask & 0x1f); if (likely(irq > 0)) do_IRQ(irq + SNI_RM200_INT_START - 1); set_c0_status(IE_IRQ0); } } void __init sni_rm200_irq_init(void) { int i; * (volatile u8 *)SNI_RM200_INT_ENA_REG = 0x1f; sni_rm200_i8259_irqs(); mips_cpu_irq_init(); /* Actually we've got more interrupts to handle ... */ for (i = SNI_RM200_INT_START; i <= SNI_RM200_INT_END; i++) irq_set_chip_and_handler(i, &rm200_irq_type, handle_level_irq); sni_hwint = sni_rm200_hwint; change_c0_status(ST0_IM, IE_IRQ0); if (request_irq(SNI_RM200_INT_START + 0, sni_rm200_i8259A_irq_handler, 0, "onboard ISA", NULL)) pr_err("Failed to register onboard ISA interrupt\n"); if (request_irq(SNI_RM200_INT_START + 1, sni_isa_irq_handler, 0, "ISA", NULL)) pr_err("Failed to register ISA interrupt\n"); } void __init sni_rm200_init(void) { }
linux-master
arch/mips/sni/rm200.c
/* * A20R specific code * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 2006 Thomas Bogendoerfer ([email protected]) */ #include <linux/init.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <linux/platform_device.h> #include <linux/serial_8250.h> #include <asm/sni.h> #include <asm/time.h> #define PORT(_base,_irq) \ { \ .iobase = _base, \ .irq = _irq, \ .uartclk = 1843200, \ .iotype = UPIO_PORT, \ .flags = UPF_BOOT_AUTOCONF, \ } static struct plat_serial8250_port a20r_data[] = { PORT(0x3f8, 4), PORT(0x2f8, 3), { }, }; static struct platform_device a20r_serial8250_device = { .name = "serial8250", .id = PLAT8250_DEV_PLATFORM, .dev = { .platform_data = a20r_data, }, }; static struct resource a20r_ds1216_rsrc[] = { { .start = 0x1c081ffc, .end = 0x1c081fff, .flags = IORESOURCE_MEM } }; static struct platform_device a20r_ds1216_device = { .name = "rtc-ds1216", .num_resources = ARRAY_SIZE(a20r_ds1216_rsrc), .resource = a20r_ds1216_rsrc }; static struct resource snirm_82596_rsrc[] = { { .start = 0x18000000, .end = 0x18000004, .flags = IORESOURCE_MEM }, { .start = 0x18010000, .end = 0x18010004, .flags = IORESOURCE_MEM }, { .start = 0x1ff00000, .end = 0x1ff00020, .flags = IORESOURCE_MEM }, { .start = 22, .end = 22, .flags = IORESOURCE_IRQ }, { .flags = 0x01 /* 16bit mpu port access */ } }; static struct platform_device snirm_82596_pdev = { .name = "snirm_82596", .num_resources = ARRAY_SIZE(snirm_82596_rsrc), .resource = snirm_82596_rsrc }; static struct resource snirm_53c710_rsrc[] = { { .start = 0x19000000, .end = 0x190fffff, .flags = IORESOURCE_MEM }, { .start = 19, .end = 19, .flags = IORESOURCE_IRQ } }; static struct platform_device snirm_53c710_pdev = { .name = "snirm_53c710", .num_resources = ARRAY_SIZE(snirm_53c710_rsrc), .resource = snirm_53c710_rsrc }; static struct resource sc26xx_rsrc[] = { { .start = 0x1c070000, .end = 0x1c0700ff, .flags = IORESOURCE_MEM }, { .start = 20, .end = 20, .flags = IORESOURCE_IRQ } }; #include <linux/platform_data/serial-sccnxp.h> static struct sccnxp_pdata sccnxp_data = { .reg_shift = 2, .mctrl_cfg[0] = MCTRL_SIG(DTR_OP, LINE_OP7) | MCTRL_SIG(RTS_OP, LINE_OP3) | MCTRL_SIG(DSR_IP, LINE_IP5) | MCTRL_SIG(DCD_IP, LINE_IP6), .mctrl_cfg[1] = MCTRL_SIG(DTR_OP, LINE_OP2) | MCTRL_SIG(RTS_OP, LINE_OP1) | MCTRL_SIG(DSR_IP, LINE_IP0) | MCTRL_SIG(CTS_IP, LINE_IP1) | MCTRL_SIG(DCD_IP, LINE_IP2) | MCTRL_SIG(RNG_IP, LINE_IP3), }; static struct platform_device sc26xx_pdev = { .name = "sc2681", .resource = sc26xx_rsrc, .num_resources = ARRAY_SIZE(sc26xx_rsrc), .dev = { .platform_data = &sccnxp_data, }, }; /* * Trigger chipset to update CPU's CAUSE IP field */ static u32 a20r_update_cause_ip(void) { u32 status = read_c0_status(); write_c0_status(status | 0x00010000); asm volatile( " .set push \n" " .set noat \n" " .set noreorder \n" " lw $1, 0(%0) \n" " sb $0, 0(%1) \n" " sync \n" " lb %1, 0(%1) \n" " b 1f \n" " ori %1, $1, 2 \n" " .align 8 \n" "1: \n" " nop \n" " sw %1, 0(%0) \n" " sync \n" " li %1, 0x20 \n" "2: \n" " nop \n" " bnez %1,2b \n" " addiu %1, -1 \n" " sw $1, 0(%0) \n" " sync \n" ".set pop \n" : : "Jr" (PCIMT_UCONF), "Jr" (0xbc000000)); write_c0_status(status); return status; } static inline void unmask_a20r_irq(struct irq_data *d) { set_c0_status(0x100 << (d->irq - SNI_A20R_IRQ_BASE)); irq_enable_hazard(); } static inline void mask_a20r_irq(struct irq_data *d) { clear_c0_status(0x100 << (d->irq - SNI_A20R_IRQ_BASE)); irq_disable_hazard(); } static struct irq_chip a20r_irq_type = { .name = "A20R", .irq_mask = mask_a20r_irq, .irq_unmask = unmask_a20r_irq, }; /* * hwint 0 receive all interrupts */ static void a20r_hwint(void) { u32 cause, status; int irq; clear_c0_status(IE_IRQ0); status = a20r_update_cause_ip(); cause = read_c0_cause(); irq = ffs(((cause & status) >> 8) & 0xf8); if (likely(irq > 0)) do_IRQ(SNI_A20R_IRQ_BASE + irq - 1); a20r_update_cause_ip(); set_c0_status(IE_IRQ0); } void __init sni_a20r_irq_init(void) { int i; for (i = SNI_A20R_IRQ_BASE + 2 ; i < SNI_A20R_IRQ_BASE + 8; i++) irq_set_chip_and_handler(i, &a20r_irq_type, handle_level_irq); sni_hwint = a20r_hwint; change_c0_status(ST0_IM, IE_IRQ0); if (request_irq(SNI_A20R_IRQ_BASE + 3, sni_isa_irq_handler, IRQF_SHARED, "ISA", sni_isa_irq_handler)) pr_err("Failed to register ISA interrupt\n"); } void sni_a20r_init(void) { /* FIXME, remove if not needed */ } static int __init snirm_a20r_setup_devinit(void) { switch (sni_brd_type) { case SNI_BRD_TOWER_OASIC: case SNI_BRD_MINITOWER: platform_device_register(&snirm_82596_pdev); platform_device_register(&snirm_53c710_pdev); platform_device_register(&sc26xx_pdev); platform_device_register(&a20r_serial8250_device); platform_device_register(&a20r_ds1216_device); sni_eisa_root_init(); break; } return 0; } device_initcall(snirm_a20r_setup_devinit);
linux-master
arch/mips/sni/a20r.c
// SPDX-License-Identifier: GPL-2.0-only /* * Virtual EISA root driver. * Acts as a placeholder if we don't have a proper EISA bridge. * * (C) 2003 Marc Zyngier <[email protected]> * modified for SNI usage by Thomas Bogendoerfer */ #include <linux/kernel.h> #include <linux/platform_device.h> #include <linux/eisa.h> #include <linux/init.h> /* The default EISA device parent (virtual root device). * Now use a platform device, since that's the obvious choice. */ static struct platform_device eisa_root_dev = { .name = "eisa", .id = 0, }; static struct eisa_root_device eisa_bus_root = { .dev = &eisa_root_dev.dev, .bus_base_addr = 0, .res = &ioport_resource, .slots = EISA_MAX_SLOTS, .dma_mask = 0xffffffff, .force_probe = 1, }; int __init sni_eisa_root_init(void) { int r; r = platform_device_register(&eisa_root_dev); if (!r) return r; dev_set_drvdata(&eisa_root_dev.dev, &eisa_bus_root); if (eisa_root_register(&eisa_bus_root)) { /* A real bridge may have been registered before * us. So quietly unregister. */ platform_device_unregister(&eisa_root_dev); return -1; } return 0; }
linux-master
arch/mips/sni/eisa.c
/* * 8250 UART probe driver for the BCM47XX platforms * Author: Aurelien Jarno * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 2007 Aurelien Jarno <[email protected]> */ #include <linux/init.h> #include <linux/serial.h> #include <linux/serial_8250.h> #include <linux/ssb/ssb.h> #include <bcm47xx.h> static struct plat_serial8250_port uart8250_data[5]; static struct platform_device uart8250_device = { .name = "serial8250", .id = PLAT8250_DEV_PLATFORM, .dev = { .platform_data = uart8250_data, }, }; #ifdef CONFIG_BCM47XX_SSB static int __init uart8250_init_ssb(void) { int i; struct ssb_mipscore *mcore = &(bcm47xx_bus.ssb.mipscore); memset(&uart8250_data, 0, sizeof(uart8250_data)); for (i = 0; i < mcore->nr_serial_ports && i < ARRAY_SIZE(uart8250_data) - 1; i++) { struct plat_serial8250_port *p = &(uart8250_data[i]); struct ssb_serial_port *ssb_port = &(mcore->serial_ports[i]); p->mapbase = (unsigned int)ssb_port->regs; p->membase = (void *)ssb_port->regs; p->irq = ssb_port->irq + 2; p->uartclk = ssb_port->baud_base; p->regshift = ssb_port->reg_shift; p->iotype = UPIO_MEM; p->flags = UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ; } return platform_device_register(&uart8250_device); } #endif #ifdef CONFIG_BCM47XX_BCMA static int __init uart8250_init_bcma(void) { int i; struct bcma_drv_cc *cc = &(bcm47xx_bus.bcma.bus.drv_cc); memset(&uart8250_data, 0, sizeof(uart8250_data)); for (i = 0; i < cc->nr_serial_ports && i < ARRAY_SIZE(uart8250_data) - 1; i++) { struct plat_serial8250_port *p = &(uart8250_data[i]); struct bcma_serial_port *bcma_port; bcma_port = &(cc->serial_ports[i]); p->mapbase = (unsigned int)bcma_port->regs; p->membase = (void *)bcma_port->regs; p->irq = bcma_port->irq; p->uartclk = bcma_port->baud_base; p->regshift = bcma_port->reg_shift; p->iotype = UPIO_MEM; p->flags = UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ; } return platform_device_register(&uart8250_device); } #endif static int __init uart8250_init(void) { switch (bcm47xx_bus_type) { #ifdef CONFIG_BCM47XX_SSB case BCM47XX_BUS_TYPE_SSB: return uart8250_init_ssb(); #endif #ifdef CONFIG_BCM47XX_BCMA case BCM47XX_BUS_TYPE_BCMA: return uart8250_init_bcma(); #endif } return -EINVAL; } device_initcall(uart8250_init);
linux-master
arch/mips/bcm47xx/serial.c
/* * Copyright (C) 2004 Florian Schirmer <[email protected]> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "bcm47xx_private.h" #include <linux/types.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <asm/setup.h> #include <asm/irq_cpu.h> #include <bcm47xx.h> asmlinkage void plat_irq_dispatch(void) { u32 cause; cause = read_c0_cause() & read_c0_status() & CAUSEF_IP; clear_c0_status(cause); if (cause & CAUSEF_IP7) do_IRQ(7); if (cause & CAUSEF_IP2) do_IRQ(2); if (cause & CAUSEF_IP3) do_IRQ(3); if (cause & CAUSEF_IP4) do_IRQ(4); if (cause & CAUSEF_IP5) do_IRQ(5); if (cause & CAUSEF_IP6) do_IRQ(6); } #define DEFINE_HWx_IRQDISPATCH(x) \ static void bcm47xx_hw ## x ## _irqdispatch(void) \ { \ do_IRQ(x); \ } DEFINE_HWx_IRQDISPATCH(2) DEFINE_HWx_IRQDISPATCH(3) DEFINE_HWx_IRQDISPATCH(4) DEFINE_HWx_IRQDISPATCH(5) DEFINE_HWx_IRQDISPATCH(6) DEFINE_HWx_IRQDISPATCH(7) void __init arch_init_irq(void) { /* * This is the first arch callback after mm_init (we can use kmalloc), * so let's finish bus initialization now. */ bcm47xx_bus_setup(); #ifdef CONFIG_BCM47XX_BCMA if (bcm47xx_bus_type == BCM47XX_BUS_TYPE_BCMA) { bcma_write32(bcm47xx_bus.bcma.bus.drv_mips.core, BCMA_MIPS_MIPS74K_INTMASK(5), 1 << 31); /* * the kernel reads the timer irq from some register and thinks * it's #5, but we offset it by 2 and route to #7 */ cp0_compare_irq = 7; } #endif mips_cpu_irq_init(); if (cpu_has_vint) { pr_info("Setting up vectored interrupts\n"); set_vi_handler(2, bcm47xx_hw2_irqdispatch); set_vi_handler(3, bcm47xx_hw3_irqdispatch); set_vi_handler(4, bcm47xx_hw4_irqdispatch); set_vi_handler(5, bcm47xx_hw5_irqdispatch); set_vi_handler(6, bcm47xx_hw6_irqdispatch); set_vi_handler(7, bcm47xx_hw7_irqdispatch); } }
linux-master
arch/mips/bcm47xx/irq.c
// SPDX-License-Identifier: GPL-2.0 #include "bcm47xx_private.h" #include <linux/gpio.h> #include <bcm47xx_board.h> #include <bcm47xx.h> static void __init bcm47xx_workarounds_enable_usb_power(int usb_power) { int err; err = gpio_request_one(usb_power, GPIOF_OUT_INIT_HIGH, "usb_power"); if (err) pr_err("Failed to request USB power gpio: %d\n", err); else gpio_free(usb_power); } void __init bcm47xx_workarounds(void) { enum bcm47xx_board board = bcm47xx_board_get(); switch (board) { case BCM47XX_BOARD_NETGEAR_WNR3500L: case BCM47XX_BOARD_NETGEAR_WNR3500L_V2: bcm47xx_workarounds_enable_usb_power(12); break; case BCM47XX_BOARD_NETGEAR_WNDR3400V2: case BCM47XX_BOARD_NETGEAR_WNDR3400_V3: bcm47xx_workarounds_enable_usb_power(21); break; default: /* No workaround(s) needed */ break; } }
linux-master
arch/mips/bcm47xx/workarounds.c
// SPDX-License-Identifier: GPL-2.0 #include "bcm47xx_private.h" #include <linux/input.h> #include <linux/gpio_keys.h> #include <linux/interrupt.h> #include <bcm47xx_board.h> #include <bcm47xx.h> /************************************************** * Database **************************************************/ #define BCM47XX_GPIO_KEY(_gpio, _code) \ { \ .code = _code, \ .gpio = _gpio, \ .active_low = 1, \ } #define BCM47XX_GPIO_KEY_H(_gpio, _code) \ { \ .code = _code, \ .gpio = _gpio, \ } /* Asus */ static const struct gpio_keys_button bcm47xx_buttons_asus_rtn10u[] __initconst = { BCM47XX_GPIO_KEY(20, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(21, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_asus_rtn12[] __initconst = { BCM47XX_GPIO_KEY(0, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(1, KEY_RESTART), BCM47XX_GPIO_KEY(4, BTN_0), /* Router mode */ BCM47XX_GPIO_KEY(5, BTN_1), /* Repeater mode */ BCM47XX_GPIO_KEY(6, BTN_2), /* AP mode */ }; static const struct gpio_keys_button bcm47xx_buttons_asus_rtn16[] __initconst = { BCM47XX_GPIO_KEY(6, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(8, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_asus_rtn66u[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(9, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_asus_wl300g[] __initconst = { BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_asus_wl320ge[] __initconst = { BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_asus_wl330ge[] __initconst = { BCM47XX_GPIO_KEY(2, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_asus_wl500g[] __initconst = { BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_asus_wl500gd[] __initconst = { BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_asus_wl500gpv1[] __initconst = { BCM47XX_GPIO_KEY(0, KEY_RESTART), BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), }; static const struct gpio_keys_button bcm47xx_buttons_asus_wl500gpv2[] __initconst = { BCM47XX_GPIO_KEY(2, KEY_RESTART), BCM47XX_GPIO_KEY(3, KEY_WPS_BUTTON), }; static const struct gpio_keys_button bcm47xx_buttons_asus_wl500w[] __initconst = { BCM47XX_GPIO_KEY_H(6, KEY_RESTART), BCM47XX_GPIO_KEY_H(7, KEY_WPS_BUTTON), }; static const struct gpio_keys_button bcm47xx_buttons_asus_wl520gc[] __initconst = { BCM47XX_GPIO_KEY(2, KEY_RESTART), BCM47XX_GPIO_KEY(3, KEY_WPS_BUTTON), }; static const struct gpio_keys_button bcm47xx_buttons_asus_wl520gu[] __initconst = { BCM47XX_GPIO_KEY(2, KEY_RESTART), BCM47XX_GPIO_KEY(3, KEY_WPS_BUTTON), }; static const struct gpio_keys_button bcm47xx_buttons_asus_wl700ge[] __initconst = { BCM47XX_GPIO_KEY(0, KEY_POWER), /* Hard disk power switch */ BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), /* EZSetup */ BCM47XX_GPIO_KEY(6, KEY_COPY), /* Copy data from USB to internal disk */ BCM47XX_GPIO_KEY(7, KEY_RESTART), /* Hard reset */ }; static const struct gpio_keys_button bcm47xx_buttons_asus_wlhdd[] __initconst = { BCM47XX_GPIO_KEY(6, KEY_RESTART), }; /* Huawei */ static const struct gpio_keys_button bcm47xx_buttons_huawei_e970[] __initconst = { BCM47XX_GPIO_KEY(6, KEY_RESTART), }; /* Belkin */ static const struct gpio_keys_button bcm47xx_buttons_belkin_f7d4301[] __initconst = { BCM47XX_GPIO_KEY(6, KEY_RESTART), BCM47XX_GPIO_KEY(8, KEY_WPS_BUTTON), }; /* Buffalo */ static const struct gpio_keys_button bcm47xx_buttons_buffalo_whr2_a54g54[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_buffalo_whr_g125[] __initconst = { BCM47XX_GPIO_KEY(0, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(4, KEY_RESTART), BCM47XX_GPIO_KEY(5, BTN_0), /* Router / AP mode swtich */ }; static const struct gpio_keys_button bcm47xx_buttons_buffalo_whr_g54s[] __initconst = { BCM47XX_GPIO_KEY(0, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY_H(4, KEY_RESTART), BCM47XX_GPIO_KEY(5, BTN_0), /* Router / AP mode swtich */ }; static const struct gpio_keys_button bcm47xx_buttons_buffalo_whr_hp_g54[] __initconst = { BCM47XX_GPIO_KEY(0, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(4, KEY_RESTART), BCM47XX_GPIO_KEY(5, BTN_0), /* Router / AP mode swtich */ }; static const struct gpio_keys_button bcm47xx_buttons_buffalo_wzr_g300n[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_buffalo_wzr_rs_g54[] __initconst = { BCM47XX_GPIO_KEY(0, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(4, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_buffalo_wzr_rs_g54hp[] __initconst = { BCM47XX_GPIO_KEY(0, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(4, KEY_RESTART), }; /* Dell */ static const struct gpio_keys_button bcm47xx_buttons_dell_tm2300[] __initconst = { BCM47XX_GPIO_KEY(0, KEY_RESTART), }; /* D-Link */ static const struct gpio_keys_button bcm47xx_buttons_dlink_dir130[] __initconst = { BCM47XX_GPIO_KEY(3, KEY_RESTART), BCM47XX_GPIO_KEY(7, KEY_UNKNOWN), }; static const struct gpio_keys_button bcm47xx_buttons_dlink_dir330[] __initconst = { BCM47XX_GPIO_KEY(3, KEY_RESTART), BCM47XX_GPIO_KEY(7, KEY_UNKNOWN), }; /* Linksys */ static const struct gpio_keys_button bcm47xx_buttons_linksys_e1000v1[] __initconst = { BCM47XX_GPIO_KEY(5, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_e1000v21[] __initconst = { BCM47XX_GPIO_KEY(9, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(10, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_e2000v1[] __initconst = { BCM47XX_GPIO_KEY(5, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(8, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_e2500v3[] __initconst = { BCM47XX_GPIO_KEY(9, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(10, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_e3000v1[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_e3200v1[] __initconst = { BCM47XX_GPIO_KEY(5, KEY_RESTART), BCM47XX_GPIO_KEY(8, KEY_WPS_BUTTON), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_e4200v1[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt150nv1[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt150nv11[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt160nv1[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt160nv3[] __initconst = { BCM47XX_GPIO_KEY(5, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt300n_v1[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt300nv11[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_UNKNOWN), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt310nv1[] __initconst = { BCM47XX_GPIO_KEY(6, KEY_RESTART), BCM47XX_GPIO_KEY(8, KEY_UNKNOWN), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt310n_v2[] __initconst = { BCM47XX_GPIO_KEY(5, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt320n_v1[] __initconst = { BCM47XX_GPIO_KEY(5, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(8, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt54g3gv2[] __initconst = { BCM47XX_GPIO_KEY(5, KEY_WIMAX), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt54g_generic[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt610nv1[] __initconst = { BCM47XX_GPIO_KEY(6, KEY_RESTART), BCM47XX_GPIO_KEY(8, KEY_WPS_BUTTON), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrt610nv2[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_linksys_wrtsl54gs[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; /* Luxul */ static const struct gpio_keys_button bcm47xx_buttons_luxul_abr_4400_v1[] = { BCM47XX_GPIO_KEY(14, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_luxul_xap_310_v1[] = { BCM47XX_GPIO_KEY(20, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_luxul_xap_1210_v1[] = { BCM47XX_GPIO_KEY(8, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_luxul_xap_1230_v1[] = { BCM47XX_GPIO_KEY(8, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_luxul_xap_1240_v1[] = { BCM47XX_GPIO_KEY(8, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_luxul_xap_1500_v1[] = { BCM47XX_GPIO_KEY(14, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_luxul_xbr_4400_v1[] = { BCM47XX_GPIO_KEY(14, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_luxul_xvw_p30_v1[] = { BCM47XX_GPIO_KEY(20, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_luxul_xwr_600_v1[] = { BCM47XX_GPIO_KEY(8, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_luxul_xwr_1750_v1[] = { BCM47XX_GPIO_KEY(14, KEY_RESTART), }; /* Microsoft */ static const struct gpio_keys_button bcm47xx_buttons_microsoft_nm700[] __initconst = { BCM47XX_GPIO_KEY(7, KEY_RESTART), }; /* Motorola */ static const struct gpio_keys_button bcm47xx_buttons_motorola_we800g[] __initconst = { BCM47XX_GPIO_KEY(0, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_motorola_wr850gp[] __initconst = { BCM47XX_GPIO_KEY(5, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_motorola_wr850gv2v3[] __initconst = { BCM47XX_GPIO_KEY(5, KEY_RESTART), }; /* Netgear */ static const struct gpio_keys_button bcm47xx_buttons_netgear_r6200_v1[] __initconst = { BCM47XX_GPIO_KEY(2, KEY_RFKILL), BCM47XX_GPIO_KEY(3, KEY_RESTART), BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), }; static const struct gpio_keys_button bcm47xx_buttons_netgear_r6300_v1[] __initconst = { BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_netgear_wn2500rp_v1[] __initconst = { BCM47XX_GPIO_KEY(12, KEY_RESTART), BCM47XX_GPIO_KEY(31, KEY_WPS_BUTTON), }; static const struct gpio_keys_button bcm47xx_buttons_netgear_wndr3400v1[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_RESTART), BCM47XX_GPIO_KEY(6, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(8, KEY_RFKILL), }; static const struct gpio_keys_button bcm47xx_buttons_netgear_wndr3400_v3[] __initconst = { BCM47XX_GPIO_KEY(12, KEY_RESTART), BCM47XX_GPIO_KEY(23, KEY_WPS_BUTTON), }; static const struct gpio_keys_button bcm47xx_buttons_netgear_wndr3700v3[] __initconst = { BCM47XX_GPIO_KEY(2, KEY_RFKILL), BCM47XX_GPIO_KEY(3, KEY_RESTART), BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), }; static const struct gpio_keys_button bcm47xx_buttons_netgear_wndr4500v1[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(5, KEY_RFKILL), BCM47XX_GPIO_KEY(6, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_netgear_wnr1000_v3[] __initconst = { BCM47XX_GPIO_KEY(2, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(3, KEY_RESTART), }; static const struct gpio_keys_button bcm47xx_buttons_netgear_wnr3500lv1[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_RESTART), BCM47XX_GPIO_KEY(6, KEY_WPS_BUTTON), }; static const struct gpio_keys_button bcm47xx_buttons_netgear_wnr3500lv2[] __initconst = { BCM47XX_GPIO_KEY(4, KEY_RESTART), BCM47XX_GPIO_KEY(6, KEY_WPS_BUTTON), BCM47XX_GPIO_KEY(8, KEY_RFKILL), }; static const struct gpio_keys_button bcm47xx_buttons_netgear_wnr834bv2[] __initconst = { BCM47XX_GPIO_KEY(6, KEY_RESTART), }; /* SimpleTech */ static const struct gpio_keys_button bcm47xx_buttons_simpletech_simpleshare[] __initconst = { BCM47XX_GPIO_KEY(0, KEY_RESTART), }; /************************************************** * Init **************************************************/ static struct gpio_keys_platform_data bcm47xx_button_pdata; static struct platform_device bcm47xx_buttons_gpio_keys = { .name = "gpio-keys", .dev = { .platform_data = &bcm47xx_button_pdata, } }; /* Copy data from __initconst */ static int __init bcm47xx_buttons_copy(const struct gpio_keys_button *buttons, size_t nbuttons) { size_t size = nbuttons * sizeof(*buttons); bcm47xx_button_pdata.buttons = kmemdup(buttons, size, GFP_KERNEL); if (!bcm47xx_button_pdata.buttons) return -ENOMEM; bcm47xx_button_pdata.nbuttons = nbuttons; return 0; } #define bcm47xx_copy_bdata(dev_buttons) \ bcm47xx_buttons_copy(dev_buttons, ARRAY_SIZE(dev_buttons)); int __init bcm47xx_buttons_register(void) { enum bcm47xx_board board = bcm47xx_board_get(); int err; switch (board) { case BCM47XX_BOARD_ASUS_RTN10U: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_rtn10u); break; case BCM47XX_BOARD_ASUS_RTN12: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_rtn12); break; case BCM47XX_BOARD_ASUS_RTN16: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_rtn16); break; case BCM47XX_BOARD_ASUS_RTN66U: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_rtn66u); break; case BCM47XX_BOARD_ASUS_WL300G: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_wl300g); break; case BCM47XX_BOARD_ASUS_WL320GE: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_wl320ge); break; case BCM47XX_BOARD_ASUS_WL330GE: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_wl330ge); break; case BCM47XX_BOARD_ASUS_WL500G: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_wl500g); break; case BCM47XX_BOARD_ASUS_WL500GD: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_wl500gd); break; case BCM47XX_BOARD_ASUS_WL500GPV1: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_wl500gpv1); break; case BCM47XX_BOARD_ASUS_WL500GPV2: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_wl500gpv2); break; case BCM47XX_BOARD_ASUS_WL500W: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_wl500w); break; case BCM47XX_BOARD_ASUS_WL520GC: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_wl520gc); break; case BCM47XX_BOARD_ASUS_WL520GU: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_wl520gu); break; case BCM47XX_BOARD_ASUS_WL700GE: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_wl700ge); break; case BCM47XX_BOARD_ASUS_WLHDD: err = bcm47xx_copy_bdata(bcm47xx_buttons_asus_wlhdd); break; case BCM47XX_BOARD_BELKIN_F7D3301: case BCM47XX_BOARD_BELKIN_F7D3302: case BCM47XX_BOARD_BELKIN_F7D4301: case BCM47XX_BOARD_BELKIN_F7D4302: case BCM47XX_BOARD_BELKIN_F7D4401: err = bcm47xx_copy_bdata(bcm47xx_buttons_belkin_f7d4301); break; case BCM47XX_BOARD_BUFFALO_WHR2_A54G54: err = bcm47xx_copy_bdata(bcm47xx_buttons_buffalo_whr2_a54g54); break; case BCM47XX_BOARD_BUFFALO_WHR_G125: err = bcm47xx_copy_bdata(bcm47xx_buttons_buffalo_whr_g125); break; case BCM47XX_BOARD_BUFFALO_WHR_G54S: err = bcm47xx_copy_bdata(bcm47xx_buttons_buffalo_whr_g54s); break; case BCM47XX_BOARD_BUFFALO_WHR_HP_G54: err = bcm47xx_copy_bdata(bcm47xx_buttons_buffalo_whr_hp_g54); break; case BCM47XX_BOARD_BUFFALO_WZR_G300N: err = bcm47xx_copy_bdata(bcm47xx_buttons_buffalo_wzr_g300n); break; case BCM47XX_BOARD_BUFFALO_WZR_RS_G54: err = bcm47xx_copy_bdata(bcm47xx_buttons_buffalo_wzr_rs_g54); break; case BCM47XX_BOARD_BUFFALO_WZR_RS_G54HP: err = bcm47xx_copy_bdata(bcm47xx_buttons_buffalo_wzr_rs_g54hp); break; case BCM47XX_BOARD_DELL_TM2300: err = bcm47xx_copy_bdata(bcm47xx_buttons_dell_tm2300); break; case BCM47XX_BOARD_DLINK_DIR130: err = bcm47xx_copy_bdata(bcm47xx_buttons_dlink_dir130); break; case BCM47XX_BOARD_DLINK_DIR330: err = bcm47xx_copy_bdata(bcm47xx_buttons_dlink_dir330); break; case BCM47XX_BOARD_HUAWEI_E970: err = bcm47xx_copy_bdata(bcm47xx_buttons_huawei_e970); break; case BCM47XX_BOARD_LINKSYS_E1000V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_e1000v1); break; case BCM47XX_BOARD_LINKSYS_E1000V21: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_e1000v21); break; case BCM47XX_BOARD_LINKSYS_E2000V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_e2000v1); break; case BCM47XX_BOARD_LINKSYS_E2500V3: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_e2500v3); break; case BCM47XX_BOARD_LINKSYS_E3000V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_e3000v1); break; case BCM47XX_BOARD_LINKSYS_E3200V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_e3200v1); break; case BCM47XX_BOARD_LINKSYS_E4200V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_e4200v1); break; case BCM47XX_BOARD_LINKSYS_WRT150NV1: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt150nv1); break; case BCM47XX_BOARD_LINKSYS_WRT150NV11: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt150nv11); break; case BCM47XX_BOARD_LINKSYS_WRT160NV1: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt160nv1); break; case BCM47XX_BOARD_LINKSYS_WRT160NV3: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt160nv3); break; case BCM47XX_BOARD_LINKSYS_WRT300N_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt300n_v1); break; case BCM47XX_BOARD_LINKSYS_WRT300NV11: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt300nv11); break; case BCM47XX_BOARD_LINKSYS_WRT310NV1: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt310nv1); break; case BCM47XX_BOARD_LINKSYS_WRT310NV2: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt310n_v2); break; case BCM47XX_BOARD_LINKSYS_WRT320N_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt320n_v1); break; case BCM47XX_BOARD_LINKSYS_WRT54G3GV2: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt54g3gv2); break; case BCM47XX_BOARD_LINKSYS_WRT54G_TYPE_0101: case BCM47XX_BOARD_LINKSYS_WRT54G_TYPE_0467: case BCM47XX_BOARD_LINKSYS_WRT54G_TYPE_0708: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt54g_generic); break; case BCM47XX_BOARD_LINKSYS_WRT610NV1: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt610nv1); break; case BCM47XX_BOARD_LINKSYS_WRT610NV2: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrt610nv2); break; case BCM47XX_BOARD_LINKSYS_WRTSL54GS: err = bcm47xx_copy_bdata(bcm47xx_buttons_linksys_wrtsl54gs); break; case BCM47XX_BOARD_LUXUL_ABR_4400_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_luxul_abr_4400_v1); break; case BCM47XX_BOARD_LUXUL_XAP_310_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_luxul_xap_310_v1); break; case BCM47XX_BOARD_LUXUL_XAP_1210_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_luxul_xap_1210_v1); break; case BCM47XX_BOARD_LUXUL_XAP_1230_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_luxul_xap_1230_v1); break; case BCM47XX_BOARD_LUXUL_XAP_1240_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_luxul_xap_1240_v1); break; case BCM47XX_BOARD_LUXUL_XAP_1500_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_luxul_xap_1500_v1); break; case BCM47XX_BOARD_LUXUL_XBR_4400_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_luxul_xbr_4400_v1); break; case BCM47XX_BOARD_LUXUL_XVW_P30_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_luxul_xvw_p30_v1); break; case BCM47XX_BOARD_LUXUL_XWR_600_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_luxul_xwr_600_v1); break; case BCM47XX_BOARD_LUXUL_XWR_1750_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_luxul_xwr_1750_v1); break; case BCM47XX_BOARD_MICROSOFT_MN700: err = bcm47xx_copy_bdata(bcm47xx_buttons_microsoft_nm700); break; case BCM47XX_BOARD_MOTOROLA_WE800G: err = bcm47xx_copy_bdata(bcm47xx_buttons_motorola_we800g); break; case BCM47XX_BOARD_MOTOROLA_WR850GP: err = bcm47xx_copy_bdata(bcm47xx_buttons_motorola_wr850gp); break; case BCM47XX_BOARD_MOTOROLA_WR850GV2V3: err = bcm47xx_copy_bdata(bcm47xx_buttons_motorola_wr850gv2v3); break; case BCM47XX_BOARD_NETGEAR_R6200_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_netgear_r6200_v1); break; case BCM47XX_BOARD_NETGEAR_R6300_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_netgear_r6300_v1); break; case BCM47XX_BOARD_NETGEAR_WN2500RP_V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_netgear_wn2500rp_v1); break; case BCM47XX_BOARD_NETGEAR_WNDR3400V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_netgear_wndr3400v1); break; case BCM47XX_BOARD_NETGEAR_WNDR3400_V3: err = bcm47xx_copy_bdata(bcm47xx_buttons_netgear_wndr3400_v3); break; case BCM47XX_BOARD_NETGEAR_WNDR3700V3: err = bcm47xx_copy_bdata(bcm47xx_buttons_netgear_wndr3700v3); break; case BCM47XX_BOARD_NETGEAR_WNDR4500V1: err = bcm47xx_copy_bdata(bcm47xx_buttons_netgear_wndr4500v1); break; case BCM47XX_BOARD_NETGEAR_WNR1000_V3: err = bcm47xx_copy_bdata(bcm47xx_buttons_netgear_wnr1000_v3); break; case BCM47XX_BOARD_NETGEAR_WNR3500L: err = bcm47xx_copy_bdata(bcm47xx_buttons_netgear_wnr3500lv1); break; case BCM47XX_BOARD_NETGEAR_WNR3500L_V2: err = bcm47xx_copy_bdata(bcm47xx_buttons_netgear_wnr3500lv2); break; case BCM47XX_BOARD_NETGEAR_WNR834BV2: err = bcm47xx_copy_bdata(bcm47xx_buttons_netgear_wnr834bv2); break; case BCM47XX_BOARD_SIMPLETECH_SIMPLESHARE: err = bcm47xx_copy_bdata(bcm47xx_buttons_simpletech_simpleshare); break; default: pr_debug("No buttons configuration found for this device\n"); return -ENOTSUPP; } if (err) return -ENOMEM; err = platform_device_register(&bcm47xx_buttons_gpio_keys); if (err) { pr_err("Failed to register platform device: %d\n", err); return err; } return 0; }
linux-master
arch/mips/bcm47xx/buttons.c
// SPDX-License-Identifier: GPL-2.0 #include <linux/errno.h> #include <linux/export.h> #include <linux/string.h> #include <bcm47xx.h> #include <bcm47xx_board.h> struct bcm47xx_board_type { const enum bcm47xx_board board; const char *name; }; struct bcm47xx_board_type_list1 { struct bcm47xx_board_type board; const char *value1; }; struct bcm47xx_board_type_list2 { struct bcm47xx_board_type board; const char *value1; const char *value2; }; struct bcm47xx_board_type_list3 { struct bcm47xx_board_type board; const char *value1; const char *value2; const char *value3; }; struct bcm47xx_board_store { enum bcm47xx_board board; char name[BCM47XX_BOARD_MAX_NAME]; }; /* model_name */ static const struct bcm47xx_board_type_list1 bcm47xx_board_list_model_name[] __initconst = { {{BCM47XX_BOARD_DLINK_DIR130, "D-Link DIR-130"}, "DIR-130"}, {{BCM47XX_BOARD_DLINK_DIR330, "D-Link DIR-330"}, "DIR-330"}, { {0}, NULL}, }; /* hardware_version */ static const struct bcm47xx_board_type_list1 bcm47xx_board_list_hardware_version[] __initconst = { {{BCM47XX_BOARD_ASUS_RTN10U, "Asus RT-N10U"}, "RTN10U"}, {{BCM47XX_BOARD_ASUS_RTN10D, "Asus RT-N10D"}, "RTN10D"}, {{BCM47XX_BOARD_ASUS_RTN12, "Asus RT-N12"}, "RT-N12"}, {{BCM47XX_BOARD_ASUS_RTN12B1, "Asus RT-N12B1"}, "RTN12B1"}, {{BCM47XX_BOARD_ASUS_RTN12C1, "Asus RT-N12C1"}, "RTN12C1"}, {{BCM47XX_BOARD_ASUS_RTN12D1, "Asus RT-N12D1"}, "RTN12D1"}, {{BCM47XX_BOARD_ASUS_RTN12HP, "Asus RT-N12HP"}, "RTN12HP"}, {{BCM47XX_BOARD_ASUS_RTN16, "Asus RT-N16"}, "RT-N16-"}, {{BCM47XX_BOARD_ASUS_WL320GE, "Asus WL320GE"}, "WL320G-"}, {{BCM47XX_BOARD_ASUS_WL330GE, "Asus WL330GE"}, "WL330GE-"}, {{BCM47XX_BOARD_ASUS_WL500GD, "Asus WL500GD"}, "WL500gd-"}, {{BCM47XX_BOARD_ASUS_WL500GPV1, "Asus WL500GP V1"}, "WL500gp-"}, {{BCM47XX_BOARD_ASUS_WL500GPV2, "Asus WL500GP V2"}, "WL500GPV2-"}, {{BCM47XX_BOARD_ASUS_WL500W, "Asus WL500W"}, "WL500gW-"}, {{BCM47XX_BOARD_ASUS_WL520GC, "Asus WL520GC"}, "WL520GC-"}, {{BCM47XX_BOARD_ASUS_WL520GU, "Asus WL520GU"}, "WL520GU-"}, {{BCM47XX_BOARD_BELKIN_F7D3301, "Belkin F7D3301"}, "F7D3301"}, {{BCM47XX_BOARD_BELKIN_F7D3302, "Belkin F7D3302"}, "F7D3302"}, {{BCM47XX_BOARD_BELKIN_F7D4301, "Belkin F7D4301"}, "F7D4301"}, {{BCM47XX_BOARD_BELKIN_F7D4302, "Belkin F7D4302"}, "F7D4302"}, {{BCM47XX_BOARD_BELKIN_F7D4401, "Belkin F7D4401"}, "F7D4401"}, { {0}, NULL}, }; /* hardware_version, boardnum */ static const struct bcm47xx_board_type_list2 bcm47xx_board_list_hw_version_num[] __initconst = { {{BCM47XX_BOARD_MICROSOFT_MN700, "Microsoft MN-700"}, "WL500-", "mn700"}, {{BCM47XX_BOARD_ASUS_WL500G, "Asus WL500G"}, "WL500-", "asusX"}, { {0}, NULL}, }; /* productid */ static const struct bcm47xx_board_type_list1 bcm47xx_board_list_productid[] __initconst = { {{BCM47XX_BOARD_ASUS_RTAC66U, "Asus RT-AC66U"}, "RT-AC66U"}, {{BCM47XX_BOARD_ASUS_RTN10, "Asus RT-N10"}, "RT-N10"}, {{BCM47XX_BOARD_ASUS_RTN10D, "Asus RT-N10D"}, "RT-N10D"}, {{BCM47XX_BOARD_ASUS_RTN15U, "Asus RT-N15U"}, "RT-N15U"}, {{BCM47XX_BOARD_ASUS_RTN16, "Asus RT-N16"}, "RT-N16"}, {{BCM47XX_BOARD_ASUS_RTN53, "Asus RT-N53"}, "RT-N53"}, {{BCM47XX_BOARD_ASUS_RTN66U, "Asus RT-N66U"}, "RT-N66U"}, {{BCM47XX_BOARD_ASUS_WL300G, "Asus WL300G"}, "WL300g"}, {{BCM47XX_BOARD_ASUS_WLHDD, "Asus WLHDD"}, "WLHDD"}, { {0}, NULL}, }; /* ModelId */ static const struct bcm47xx_board_type_list1 bcm47xx_board_list_ModelId[] __initconst = { {{BCM47XX_BOARD_DELL_TM2300, "Dell TrueMobile 2300"}, "WX-5565"}, {{BCM47XX_BOARD_MOTOROLA_WE800G, "Motorola WE800G"}, "WE800G"}, {{BCM47XX_BOARD_MOTOROLA_WR850GP, "Motorola WR850GP"}, "WR850GP"}, {{BCM47XX_BOARD_MOTOROLA_WR850GV2V3, "Motorola WR850G"}, "WR850G"}, { {0}, NULL}, }; /* melco_id or buf1falo_id */ static const struct bcm47xx_board_type_list1 bcm47xx_board_list_melco_id[] __initconst = { {{BCM47XX_BOARD_BUFFALO_WBR2_G54, "Buffalo WBR2-G54"}, "29bb0332"}, {{BCM47XX_BOARD_BUFFALO_WHR2_A54G54, "Buffalo WHR2-A54G54"}, "290441dd"}, {{BCM47XX_BOARD_BUFFALO_WHR_G125, "Buffalo WHR-G125"}, "32093"}, {{BCM47XX_BOARD_BUFFALO_WHR_G54S, "Buffalo WHR-G54S"}, "30182"}, {{BCM47XX_BOARD_BUFFALO_WHR_HP_G54, "Buffalo WHR-HP-G54"}, "30189"}, {{BCM47XX_BOARD_BUFFALO_WLA2_G54L, "Buffalo WLA2-G54L"}, "29129"}, {{BCM47XX_BOARD_BUFFALO_WZR_G300N, "Buffalo WZR-G300N"}, "31120"}, {{BCM47XX_BOARD_BUFFALO_WZR_RS_G54, "Buffalo WZR-RS-G54"}, "30083"}, {{BCM47XX_BOARD_BUFFALO_WZR_RS_G54HP, "Buffalo WZR-RS-G54HP"}, "30103"}, { {0}, NULL}, }; /* boot_hw_model, boot_hw_ver */ static const struct bcm47xx_board_type_list2 bcm47xx_board_list_boot_hw[] __initconst = { /* like WRT160N v3.0 */ {{BCM47XX_BOARD_CISCO_M10V1, "Cisco M10"}, "M10", "1.0"}, /* like WRT310N v2.0 */ {{BCM47XX_BOARD_CISCO_M20V1, "Cisco M20"}, "M20", "1.0"}, {{BCM47XX_BOARD_LINKSYS_E900V1, "Linksys E900 V1"}, "E900", "1.0"}, /* like WRT160N v3.0 */ {{BCM47XX_BOARD_LINKSYS_E1000V1, "Linksys E1000 V1"}, "E100", "1.0"}, {{BCM47XX_BOARD_LINKSYS_E1000V2, "Linksys E1000 V2"}, "E1000", "2.0"}, {{BCM47XX_BOARD_LINKSYS_E1000V21, "Linksys E1000 V2.1"}, "E1000", "2.1"}, {{BCM47XX_BOARD_LINKSYS_E1200V2, "Linksys E1200 V2"}, "E1200", "2.0"}, {{BCM47XX_BOARD_LINKSYS_E2000V1, "Linksys E2000 V1"}, "Linksys E2000", "1.0"}, {{BCM47XX_BOARD_LINKSYS_E2500V3, "Linksys E2500 V3"}, "E2500", "1.0"}, /* like WRT610N v2.0 */ {{BCM47XX_BOARD_LINKSYS_E3000V1, "Linksys E3000 V1"}, "E300", "1.0"}, {{BCM47XX_BOARD_LINKSYS_E3200V1, "Linksys E3200 V1"}, "E3200", "1.0"}, {{BCM47XX_BOARD_LINKSYS_E4200V1, "Linksys E4200 V1"}, "E4200", "1.0"}, {{BCM47XX_BOARD_LINKSYS_WRT150NV11, "Linksys WRT150N V1.1"}, "WRT150N", "1.1"}, {{BCM47XX_BOARD_LINKSYS_WRT150NV1, "Linksys WRT150N V1"}, "WRT150N", "1"}, {{BCM47XX_BOARD_LINKSYS_WRT160NV1, "Linksys WRT160N V1"}, "WRT160N", "1.0"}, {{BCM47XX_BOARD_LINKSYS_WRT160NV3, "Linksys WRT160N V3"}, "WRT160N", "3.0"}, {{BCM47XX_BOARD_LINKSYS_WRT300NV11, "Linksys WRT300N V1.1"}, "WRT300N", "1.1"}, {{BCM47XX_BOARD_LINKSYS_WRT310NV1, "Linksys WRT310N V1"}, "WRT310N", "1.0"}, {{BCM47XX_BOARD_LINKSYS_WRT310NV2, "Linksys WRT310N V2"}, "WRT310N", "2.0"}, {{BCM47XX_BOARD_LINKSYS_WRT320N_V1, "Linksys WRT320N V1"}, "WRT320N", "1.0"}, {{BCM47XX_BOARD_LINKSYS_WRT54G3GV2, "Linksys WRT54G3GV2-VF"}, "WRT54G3GV2-VF", "1.0"}, {{BCM47XX_BOARD_LINKSYS_WRT610NV1, "Linksys WRT610N V1"}, "WRT610N", "1.0"}, {{BCM47XX_BOARD_LINKSYS_WRT610NV2, "Linksys WRT610N V2"}, "WRT610N", "2.0"}, { {0}, NULL}, }; /* board_id */ static const struct bcm47xx_board_type_list1 bcm47xx_board_list_board_id[] __initconst = { {{BCM47XX_BOARD_LUXUL_ABR_4400_V1, "Luxul ABR-4400 V1"}, "luxul_abr4400_v1"}, {{BCM47XX_BOARD_LUXUL_XAP_310_V1, "Luxul XAP-310 V1"}, "luxul_xap310_v1"}, {{BCM47XX_BOARD_LUXUL_XAP_1210_V1, "Luxul XAP-1210 V1"}, "luxul_xap1210_v1"}, {{BCM47XX_BOARD_LUXUL_XAP_1230_V1, "Luxul XAP-1230 V1"}, "luxul_xap1230_v1"}, {{BCM47XX_BOARD_LUXUL_XAP_1240_V1, "Luxul XAP-1240 V1"}, "luxul_xap1240_v1"}, {{BCM47XX_BOARD_LUXUL_XAP_1500_V1, "Luxul XAP-1500 V1"}, "luxul_xap1500_v1"}, {{BCM47XX_BOARD_LUXUL_XBR_4400_V1, "Luxul XBR-4400 V1"}, "luxul_xbr4400_v1"}, {{BCM47XX_BOARD_LUXUL_XVW_P30_V1, "Luxul XVW-P30 V1"}, "luxul_xvwp30_v1"}, {{BCM47XX_BOARD_LUXUL_XWR_600_V1, "Luxul XWR-600 V1"}, "luxul_xwr600_v1"}, {{BCM47XX_BOARD_LUXUL_XWR_1750_V1, "Luxul XWR-1750 V1"}, "luxul_xwr1750_v1"}, {{BCM47XX_BOARD_NETGEAR_R6200_V1, "Netgear R6200 V1"}, "U12H192T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_R6300_V1, "Netgear R6300 V1"}, "U12H218T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WGR614V8, "Netgear WGR614 V8"}, "U12H072T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WGR614V9, "Netgear WGR614 V9"}, "U12H094T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WGR614_V10, "Netgear WGR614 V10"}, "U12H139T01_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WN2500RP_V1, "Netgear WN2500RP V1"}, "U12H197T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WN2500RP_V2, "Netgear WN2500RP V2"}, "U12H294T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNDR3300, "Netgear WNDR3300"}, "U12H093T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNDR3400V1, "Netgear WNDR3400 V1"}, "U12H155T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNDR3400V2, "Netgear WNDR3400 V2"}, "U12H187T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNDR3400_V3, "Netgear WNDR3400 V3"}, "U12H208T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNDR3400VCNA, "Netgear WNDR3400 Vcna"}, "U12H155T01_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNDR3700V3, "Netgear WNDR3700 V3"}, "U12H194T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNDR4000, "Netgear WNDR4000"}, "U12H181T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNDR4500V1, "Netgear WNDR4500 V1"}, "U12H189T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNDR4500V2, "Netgear WNDR4500 V2"}, "U12H224T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNR1000_V3, "Netgear WNR1000 V3"}, "U12H139T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNR1000_V3, "Netgear WNR1000 V3"}, "U12H139T50_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNR2000, "Netgear WNR2000"}, "U12H114T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNR3500L, "Netgear WNR3500L"}, "U12H136T99_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNR3500L_V2, "Netgear WNR3500L V2"}, "U12H172T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNR3500U, "Netgear WNR3500U"}, "U12H136T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNR3500V2, "Netgear WNR3500 V2"}, "U12H127T00_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNR3500V2VC, "Netgear WNR3500 V2vc"}, "U12H127T70_NETGEAR"}, {{BCM47XX_BOARD_NETGEAR_WNR834BV2, "Netgear WNR834B V2"}, "U12H081T00_NETGEAR"}, { {0}, NULL}, }; /* boardtype, boardnum, boardrev */ static const struct bcm47xx_board_type_list3 bcm47xx_board_list_board[] __initconst = { {{BCM47XX_BOARD_HUAWEI_B593U_12, "Huawei B593u-12"}, "0x053d", "1234", "0x1301"}, {{BCM47XX_BOARD_HUAWEI_E970, "Huawei E970"}, "0x048e", "0x5347", "0x11"}, {{BCM47XX_BOARD_PHICOMM_M1, "Phicomm M1"}, "0x0590", "80", "0x1104"}, {{BCM47XX_BOARD_ZTE_H218N, "ZTE H218N"}, "0x053d", "1234", "0x1305"}, {{BCM47XX_BOARD_NETGEAR_WNR3500L, "Netgear WNR3500L"}, "0x04CF", "3500", "02"}, {{BCM47XX_BOARD_NETGEAR_WNR3500L_V2, "Netgear WNR3500L V2"}, "0x052b", "3500L", "02"}, {{BCM47XX_BOARD_LINKSYS_WRT54G_TYPE_0101, "Linksys WRT54G/GS/GL"}, "0x0101", "42", "0x10"}, {{BCM47XX_BOARD_LINKSYS_WRT54G_TYPE_0467, "Linksys WRT54G/GS/GL"}, "0x0467", "42", "0x10"}, {{BCM47XX_BOARD_LINKSYS_WRT54G_TYPE_0708, "Linksys WRT54G/GS/GL"}, "0x0708", "42", "0x10"}, { {0}, NULL}, }; /* boardtype, boardrev */ static const struct bcm47xx_board_type_list2 bcm47xx_board_list_board_type_rev[] __initconst = { {{BCM47XX_BOARD_SIEMENS_SE505V2, "Siemens SE505 V2"}, "0x0101", "0x10"}, { {0}, NULL}, }; /* * Some devices don't use any common NVRAM entry for identification and they * have only one model specific variable. * They don't deserve own arrays, let's group them there using key-value array. */ static const struct bcm47xx_board_type_list2 bcm47xx_board_list_key_value[] __initconst = { {{BCM47XX_BOARD_ASUS_WL700GE, "Asus WL700"}, "model_no", "WL700"}, {{BCM47XX_BOARD_LINKSYS_WRT300N_V1, "Linksys WRT300N V1"}, "router_name", "WRT300N"}, {{BCM47XX_BOARD_LINKSYS_WRT600N_V11, "Linksys WRT600N V1.1"}, "Model_Name", "WRT600N"}, {{BCM47XX_BOARD_LINKSYS_WRTSL54GS, "Linksys WRTSL54GS"}, "machine_name", "WRTSL54GS"}, { {0}, NULL}, }; static const struct bcm47xx_board_type bcm47xx_board_unknown[] __initconst = { {BCM47XX_BOARD_UNKNOWN, "Unknown Board"}, }; static struct bcm47xx_board_store bcm47xx_board = {BCM47XX_BOARD_NO, "Unknown Board"}; static __init const struct bcm47xx_board_type *bcm47xx_board_get_nvram(void) { char buf1[30]; char buf2[30]; char buf3[30]; const struct bcm47xx_board_type_list1 *e1; const struct bcm47xx_board_type_list2 *e2; const struct bcm47xx_board_type_list3 *e3; if (bcm47xx_nvram_getenv("model_name", buf1, sizeof(buf1)) >= 0) { for (e1 = bcm47xx_board_list_model_name; e1->value1; e1++) { if (!strcmp(buf1, e1->value1)) return &e1->board; } } if (bcm47xx_nvram_getenv("hardware_version", buf1, sizeof(buf1)) >= 0) { for (e1 = bcm47xx_board_list_hardware_version; e1->value1; e1++) { if (strstarts(buf1, e1->value1)) return &e1->board; } } if (bcm47xx_nvram_getenv("hardware_version", buf1, sizeof(buf1)) >= 0 && bcm47xx_nvram_getenv("boardnum", buf2, sizeof(buf2)) >= 0) { for (e2 = bcm47xx_board_list_hw_version_num; e2->value1; e2++) { if (!strstarts(buf1, e2->value1) && !strcmp(buf2, e2->value2)) return &e2->board; } } if (bcm47xx_nvram_getenv("productid", buf1, sizeof(buf1)) >= 0) { for (e1 = bcm47xx_board_list_productid; e1->value1; e1++) { if (!strcmp(buf1, e1->value1)) return &e1->board; } } if (bcm47xx_nvram_getenv("ModelId", buf1, sizeof(buf1)) >= 0) { for (e1 = bcm47xx_board_list_ModelId; e1->value1; e1++) { if (!strcmp(buf1, e1->value1)) return &e1->board; } } if (bcm47xx_nvram_getenv("melco_id", buf1, sizeof(buf1)) >= 0 || bcm47xx_nvram_getenv("buf1falo_id", buf1, sizeof(buf1)) >= 0) { /* buffalo hardware, check id for specific hardware matches */ for (e1 = bcm47xx_board_list_melco_id; e1->value1; e1++) { if (!strcmp(buf1, e1->value1)) return &e1->board; } } if (bcm47xx_nvram_getenv("boot_hw_model", buf1, sizeof(buf1)) >= 0 && bcm47xx_nvram_getenv("boot_hw_ver", buf2, sizeof(buf2)) >= 0) { for (e2 = bcm47xx_board_list_boot_hw; e2->value1; e2++) { if (!strcmp(buf1, e2->value1) && !strcmp(buf2, e2->value2)) return &e2->board; } } if (bcm47xx_nvram_getenv("board_id", buf1, sizeof(buf1)) >= 0) { for (e1 = bcm47xx_board_list_board_id; e1->value1; e1++) { if (!strcmp(buf1, e1->value1)) return &e1->board; } } if (bcm47xx_nvram_getenv("boardtype", buf1, sizeof(buf1)) >= 0 && bcm47xx_nvram_getenv("boardnum", buf2, sizeof(buf2)) >= 0 && bcm47xx_nvram_getenv("boardrev", buf3, sizeof(buf3)) >= 0) { for (e3 = bcm47xx_board_list_board; e3->value1; e3++) { if (!strcmp(buf1, e3->value1) && !strcmp(buf2, e3->value2) && !strcmp(buf3, e3->value3)) return &e3->board; } } if (bcm47xx_nvram_getenv("boardtype", buf1, sizeof(buf1)) >= 0 && bcm47xx_nvram_getenv("boardrev", buf2, sizeof(buf2)) >= 0 && bcm47xx_nvram_getenv("boardnum", buf3, sizeof(buf3)) == -ENOENT) { for (e2 = bcm47xx_board_list_board_type_rev; e2->value1; e2++) { if (!strcmp(buf1, e2->value1) && !strcmp(buf2, e2->value2)) return &e2->board; } } for (e2 = bcm47xx_board_list_key_value; e2->value1; e2++) { if (bcm47xx_nvram_getenv(e2->value1, buf1, sizeof(buf1)) >= 0) { if (!strcmp(buf1, e2->value2)) return &e2->board; } } return bcm47xx_board_unknown; } void __init bcm47xx_board_detect(void) { int err; char buf[10]; const struct bcm47xx_board_type *board_detected; if (bcm47xx_board.board != BCM47XX_BOARD_NO) return; /* check if the nvram is available */ err = bcm47xx_nvram_getenv("boardtype", buf, sizeof(buf)); /* init of nvram failed, probably too early now */ if (err == -ENXIO) return; board_detected = bcm47xx_board_get_nvram(); bcm47xx_board.board = board_detected->board; strscpy(bcm47xx_board.name, board_detected->name, BCM47XX_BOARD_MAX_NAME); } enum bcm47xx_board bcm47xx_board_get(void) { return bcm47xx_board.board; } EXPORT_SYMBOL(bcm47xx_board_get); const char *bcm47xx_board_get_name(void) { return bcm47xx_board.name; } EXPORT_SYMBOL(bcm47xx_board_get_name);
linux-master
arch/mips/bcm47xx/board.c
/* * Copyright (C) 2004 Florian Schirmer <[email protected]> * Copyright (C) 2006 Felix Fietkau <[email protected]> * Copyright (C) 2006 Michael Buesch <[email protected]> * Copyright (C) 2010 Waldemar Brodkorb <[email protected]> * Copyright (C) 2010-2012 Hauke Mehrtens <[email protected]> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "bcm47xx_private.h" #include <linux/bcm47xx_sprom.h> #include <linux/export.h> #include <linux/types.h> #include <linux/ethtool.h> #include <linux/phy.h> #include <linux/phy_fixed.h> #include <linux/ssb/ssb.h> #include <linux/ssb/ssb_embedded.h> #include <linux/bcma/bcma_soc.h> #include <asm/bootinfo.h> #include <asm/idle.h> #include <asm/prom.h> #include <asm/reboot.h> #include <asm/time.h> #include <bcm47xx.h> #include <bcm47xx_board.h> union bcm47xx_bus bcm47xx_bus; EXPORT_SYMBOL(bcm47xx_bus); enum bcm47xx_bus_type bcm47xx_bus_type; EXPORT_SYMBOL(bcm47xx_bus_type); static void bcm47xx_machine_restart(char *command) { pr_alert("Please stand by while rebooting the system...\n"); local_irq_disable(); /* Set the watchdog timer to reset immediately */ switch (bcm47xx_bus_type) { #ifdef CONFIG_BCM47XX_SSB case BCM47XX_BUS_TYPE_SSB: if (bcm47xx_bus.ssb.chip_id == 0x4785) write_c0_diag4(1 << 22); ssb_watchdog_timer_set(&bcm47xx_bus.ssb, 1); if (bcm47xx_bus.ssb.chip_id == 0x4785) { __asm__ __volatile__( ".set\tmips3\n\t" "sync\n\t" "wait\n\t" ".set\tmips0"); } break; #endif #ifdef CONFIG_BCM47XX_BCMA case BCM47XX_BUS_TYPE_BCMA: bcma_chipco_watchdog_timer_set(&bcm47xx_bus.bcma.bus.drv_cc, 1); break; #endif } while (1) cpu_relax(); } static void bcm47xx_machine_halt(void) { /* Disable interrupts and watchdog and spin forever */ local_irq_disable(); switch (bcm47xx_bus_type) { #ifdef CONFIG_BCM47XX_SSB case BCM47XX_BUS_TYPE_SSB: ssb_watchdog_timer_set(&bcm47xx_bus.ssb, 0); break; #endif #ifdef CONFIG_BCM47XX_BCMA case BCM47XX_BUS_TYPE_BCMA: bcma_chipco_watchdog_timer_set(&bcm47xx_bus.bcma.bus.drv_cc, 0); break; #endif } while (1) cpu_relax(); } #ifdef CONFIG_BCM47XX_SSB static void __init bcm47xx_register_ssb(void) { int err; char buf[100]; struct ssb_mipscore *mcore; err = ssb_bus_host_soc_register(&bcm47xx_bus.ssb, SSB_ENUM_BASE); if (err) panic("Failed to initialize SSB bus (err %d)", err); mcore = &bcm47xx_bus.ssb.mipscore; if (bcm47xx_nvram_getenv("kernel_args", buf, sizeof(buf)) >= 0) { if (strstr(buf, "console=ttyS1")) { struct ssb_serial_port port; pr_debug("Swapping serial ports!\n"); /* swap serial ports */ memcpy(&port, &mcore->serial_ports[0], sizeof(port)); memcpy(&mcore->serial_ports[0], &mcore->serial_ports[1], sizeof(port)); memcpy(&mcore->serial_ports[1], &port, sizeof(port)); } } } #endif #ifdef CONFIG_BCM47XX_BCMA static void __init bcm47xx_register_bcma(void) { int err; err = bcma_host_soc_register(&bcm47xx_bus.bcma); if (err) panic("Failed to register BCMA bus (err %d)", err); } #endif /* * Memory setup is done in the early part of MIPS's arch_mem_init. It's supposed * to detect memory and record it with memblock_add. * Any extra initializaion performed here must not use kmalloc or bootmem. */ void __init plat_mem_setup(void) { struct cpuinfo_mips *c = &current_cpu_data; if (c->cputype == CPU_74K) { pr_info("Using bcma bus\n"); #ifdef CONFIG_BCM47XX_BCMA bcm47xx_bus_type = BCM47XX_BUS_TYPE_BCMA; bcm47xx_register_bcma(); bcm47xx_set_system_type(bcm47xx_bus.bcma.bus.chipinfo.id); #ifdef CONFIG_HIGHMEM bcm47xx_prom_highmem_init(); #endif #endif } else { pr_info("Using ssb bus\n"); #ifdef CONFIG_BCM47XX_SSB bcm47xx_bus_type = BCM47XX_BUS_TYPE_SSB; bcm47xx_sprom_register_fallbacks(); bcm47xx_register_ssb(); bcm47xx_set_system_type(bcm47xx_bus.ssb.chip_id); #endif } _machine_restart = bcm47xx_machine_restart; _machine_halt = bcm47xx_machine_halt; pm_power_off = bcm47xx_machine_halt; } #ifdef CONFIG_BCM47XX_BCMA static struct device * __init bcm47xx_setup_device(void) { struct device *dev; int err; dev = kzalloc(sizeof(*dev), GFP_KERNEL); if (!dev) return NULL; err = dev_set_name(dev, "bcm47xx_soc"); if (err) { pr_err("Failed to set SoC device name: %d\n", err); kfree(dev); return NULL; } err = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32)); if (err) pr_err("Failed to set SoC DMA mask: %d\n", err); return dev; } #endif /* * This finishes bus initialization doing things that were not possible without * kmalloc. Make sure to call it late enough (after mm_init). */ void __init bcm47xx_bus_setup(void) { #ifdef CONFIG_BCM47XX_BCMA if (bcm47xx_bus_type == BCM47XX_BUS_TYPE_BCMA) { int err; bcm47xx_bus.bcma.dev = bcm47xx_setup_device(); if (!bcm47xx_bus.bcma.dev) panic("Failed to setup SoC device\n"); err = bcma_host_soc_init(&bcm47xx_bus.bcma); if (err) panic("Failed to initialize BCMA bus (err %d)", err); } #endif /* With bus initialized we can access NVRAM and detect the board */ bcm47xx_board_detect(); mips_set_machine_name(bcm47xx_board_get_name()); } static int __init bcm47xx_cpu_fixes(void) { switch (bcm47xx_bus_type) { #ifdef CONFIG_BCM47XX_SSB case BCM47XX_BUS_TYPE_SSB: /* Nothing to do */ break; #endif #ifdef CONFIG_BCM47XX_BCMA case BCM47XX_BUS_TYPE_BCMA: /* The BCM4706 has a problem with the CPU wait instruction. * When r4k_wait or r4k_wait_irqoff is used will just hang and * not return from a msleep(). Removing the cpu_wait * functionality is a workaround for this problem. The BCM4716 * does not have this problem. */ if (bcm47xx_bus.bcma.bus.chipinfo.id == BCMA_CHIP_ID_BCM4706) cpu_wait = NULL; break; #endif } return 0; } arch_initcall(bcm47xx_cpu_fixes); static struct fixed_phy_status bcm47xx_fixed_phy_status __initdata = { .link = 1, .speed = SPEED_100, .duplex = DUPLEX_FULL, }; static int __init bcm47xx_register_bus_complete(void) { switch (bcm47xx_bus_type) { #ifdef CONFIG_BCM47XX_SSB case BCM47XX_BUS_TYPE_SSB: /* Nothing to do */ break; #endif #ifdef CONFIG_BCM47XX_BCMA case BCM47XX_BUS_TYPE_BCMA: if (device_register(bcm47xx_bus.bcma.dev)) pr_err("Failed to register SoC device\n"); bcma_bus_register(&bcm47xx_bus.bcma.bus); break; #endif } bcm47xx_buttons_register(); bcm47xx_leds_register(); bcm47xx_workarounds(); fixed_phy_add(PHY_POLL, 0, &bcm47xx_fixed_phy_status); return 0; } device_initcall(bcm47xx_register_bus_complete);
linux-master
arch/mips/bcm47xx/setup.c
/* * Copyright (C) 2004 Florian Schirmer <[email protected]> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <linux/init.h> #include <linux/ssb/ssb.h> #include <asm/time.h> #include <bcm47xx.h> #include <bcm47xx_board.h> void __init plat_time_init(void) { unsigned long hz = 0; u16 chip_id = 0; char buf[10]; int len; enum bcm47xx_board board = bcm47xx_board_get(); /* * Use deterministic values for initial counter interrupt * so that calibrate delay avoids encountering a counter wrap. */ write_c0_count(0); write_c0_compare(0xffff); switch (bcm47xx_bus_type) { #ifdef CONFIG_BCM47XX_SSB case BCM47XX_BUS_TYPE_SSB: hz = ssb_cpu_clock(&bcm47xx_bus.ssb.mipscore) / 2; chip_id = bcm47xx_bus.ssb.chip_id; break; #endif #ifdef CONFIG_BCM47XX_BCMA case BCM47XX_BUS_TYPE_BCMA: hz = bcma_cpu_clock(&bcm47xx_bus.bcma.bus.drv_mips) / 2; chip_id = bcm47xx_bus.bcma.bus.chipinfo.id; break; #endif } if (chip_id == 0x5354) { len = bcm47xx_nvram_getenv("clkfreq", buf, sizeof(buf)); if (len >= 0 && !strncmp(buf, "200", 4)) hz = 100000000; } switch (board) { case BCM47XX_BOARD_ASUS_WL520GC: case BCM47XX_BOARD_ASUS_WL520GU: hz = 100000000; break; default: break; } if (!hz) hz = 100000000; /* Set MIPS counter frequency for fixed_rate_gettimeoffset() */ mips_hpt_frequency = hz; }
linux-master
arch/mips/bcm47xx/time.c
/* * Copyright (C) 2004 Florian Schirmer <[email protected]> * Copyright (C) 2007 Aurelien Jarno <[email protected]> * Copyright (C) 2010-2012 Hauke Mehrtens <[email protected]> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <linux/init.h> #include <linux/types.h> #include <linux/kernel.h> #include <linux/memblock.h> #include <linux/spinlock.h> #include <linux/ssb/ssb_driver_chipcommon.h> #include <linux/ssb/ssb_regs.h> #include <linux/smp.h> #include <asm/bootinfo.h> #include <bcm47xx.h> #include <bcm47xx_board.h> static char bcm47xx_system_type[20] = "Broadcom BCM47XX"; const char *get_system_type(void) { return bcm47xx_system_type; } __init void bcm47xx_set_system_type(u16 chip_id) { snprintf(bcm47xx_system_type, sizeof(bcm47xx_system_type), (chip_id > 0x9999) ? "Broadcom BCM%d" : "Broadcom BCM%04X", chip_id); } static unsigned long lowmem __initdata; static __init void prom_init_mem(void) { unsigned long mem; unsigned long max; unsigned long off; struct cpuinfo_mips *c = &current_cpu_data; /* Figure out memory size by finding aliases. * * We should theoretically use the mapping from CFE using cfe_enummem(). * However as the BCM47XX is mostly used on low-memory systems, we * want to reuse the memory used by CFE (around 4MB). That means cfe_* * functions stop to work at some point during the boot, we should only * call them at the beginning of the boot. * * BCM47XX uses 128MB for addressing the ram, if the system contains * less than that amount of ram it remaps the ram more often into the * available space. */ /* Physical address, without mapping to any kernel segment */ off = CPHYSADDR((unsigned long)prom_init); /* Accessing memory after 128 MiB will cause an exception */ max = 128 << 20; for (mem = 1 << 20; mem < max; mem += 1 << 20) { /* Loop condition may be not enough, off may be over 1 MiB */ if (off + mem >= max) { mem = max; pr_debug("Assume 128MB RAM\n"); break; } if (!memcmp((void *)prom_init, (void *)prom_init + mem, 32)) break; } lowmem = mem; /* Ignoring the last page when ddr size is 128M. Cached * accesses to last page is causing the processor to prefetch * using address above 128M stepping out of the ddr address * space. */ if (c->cputype == CPU_74K && (mem == (128 << 20))) mem -= 0x1000; memblock_add(0, mem); } /* * This is the first serial on the chip common core, it is at this position * for sb (ssb) and ai (bcma) bus. */ #define BCM47XX_SERIAL_ADDR (SSB_ENUM_BASE + SSB_CHIPCO_UART0_DATA) void __init prom_init(void) { prom_init_mem(); setup_8250_early_printk_port(CKSEG1ADDR(BCM47XX_SERIAL_ADDR), 0, 0); } #if defined(CONFIG_BCM47XX_BCMA) && defined(CONFIG_HIGHMEM) #define EXTVBASE 0xc0000000 #define ENTRYLO(x) ((pte_val(pfn_pte((x) >> PFN_PTE_SHIFT, PAGE_KERNEL_UNCACHED)) >> 6) | 1) #include <asm/tlbflush.h> /* Stripped version of tlb_init, with the call to build_tlb_refill_handler * dropped. Calling it at this stage causes a hang. */ void early_tlb_init(void) { write_c0_pagemask(PM_DEFAULT_MASK); write_c0_wired(0); temp_tlb_entry = current_cpu_data.tlbsize - 1; local_flush_tlb_all(); } void __init bcm47xx_prom_highmem_init(void) { unsigned long off = (unsigned long)prom_init; unsigned long extmem = 0; bool highmem_region = false; if (WARN_ON(bcm47xx_bus_type != BCM47XX_BUS_TYPE_BCMA)) return; if (bcm47xx_bus.bcma.bus.chipinfo.id == BCMA_CHIP_ID_BCM4706) highmem_region = true; if (lowmem != 128 << 20 || !highmem_region) return; early_tlb_init(); /* Add one temporary TLB entry to map SDRAM Region 2. * Physical Virtual * 0x80000000 0xc0000000 (1st: 256MB) * 0x90000000 0xd0000000 (2nd: 256MB) */ add_temporary_entry(ENTRYLO(0x80000000), ENTRYLO(0x80000000 + (256 << 20)), EXTVBASE, PM_256M); off = EXTVBASE + __pa(off); for (extmem = 128 << 20; extmem < 512 << 20; extmem <<= 1) { if (!memcmp((void *)prom_init, (void *)(off + extmem), 16)) break; } extmem -= lowmem; early_tlb_init(); if (!extmem) return; pr_warn("Found %lu MiB of extra memory, but highmem is unsupported yet!\n", extmem >> 20); /* TODO: Register extra memory */ } #endif /* defined(CONFIG_BCM47XX_BCMA) && defined(CONFIG_HIGHMEM) */
linux-master
arch/mips/bcm47xx/prom.c
// SPDX-License-Identifier: GPL-2.0 #include "bcm47xx_private.h" #include <linux/leds.h> #include <bcm47xx_board.h> /************************************************** * Database **************************************************/ #define BCM47XX_GPIO_LED(_gpio, _color, _function, _active_low, \ _default_state) \ { \ .name = "bcm47xx:" _color ":" _function, \ .gpio = _gpio, \ .active_low = _active_low, \ .default_state = _default_state, \ } #define BCM47XX_GPIO_LED_TRIGGER(_gpio, _color, _function, _active_low, \ _default_trigger) \ { \ .name = "bcm47xx:" _color ":" _function, \ .gpio = _gpio, \ .active_low = _active_low, \ .default_state = LEDS_GPIO_DEFSTATE_OFF, \ .default_trigger = _default_trigger, \ } /* Asus */ static const struct gpio_led bcm47xx_leds_asus_rtn10u[] __initconst = { BCM47XX_GPIO_LED(5, "green", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(6, "green", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(7, "green", "wps", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(8, "green", "usb", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_asus_rtn12[] __initconst = { BCM47XX_GPIO_LED(2, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(7, "unk", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_asus_rtn15u[] __initconst = { /* TODO: Add "wlan" LED */ BCM47XX_GPIO_LED(3, "blue", "wan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(4, "blue", "lan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(6, "blue", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(9, "blue", "usb", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_asus_rtn16[] __initconst = { BCM47XX_GPIO_LED(1, "blue", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(7, "blue", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_asus_rtn66u[] __initconst = { BCM47XX_GPIO_LED(12, "blue", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(15, "blue", "usb", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_asus_wl300g[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), }; static const struct gpio_led bcm47xx_leds_asus_wl320ge[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(2, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(11, "unk", "link", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_asus_wl330ge[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), }; static const struct gpio_led bcm47xx_leds_asus_wl500g[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), }; static const struct gpio_led bcm47xx_leds_asus_wl500gd[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), }; static const struct gpio_led bcm47xx_leds_asus_wl500gpv1[] __initconst = { BCM47XX_GPIO_LED(1, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), }; static const struct gpio_led bcm47xx_leds_asus_wl500gpv2[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(1, "unk", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_asus_wl500w[] __initconst = { BCM47XX_GPIO_LED(5, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), }; static const struct gpio_led bcm47xx_leds_asus_wl520gc[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(1, "unk", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_asus_wl520gu[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(1, "unk", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_asus_wl700ge[] __initconst = { BCM47XX_GPIO_LED(1, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), /* Labeled "READY" (there is no "power" LED). Originally ON, flashing on USB activity. */ }; static const struct gpio_led bcm47xx_leds_asus_wlhdd[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(2, "unk", "usb", 1, LEDS_GPIO_DEFSTATE_OFF), }; /* Belkin */ static const struct gpio_led bcm47xx_leds_belkin_f7d4301[] __initconst = { BCM47XX_GPIO_LED(10, "green", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(11, "amber", "power", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(12, "unk", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(13, "unk", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(14, "unk", "usb0", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(15, "unk", "usb1", 1, LEDS_GPIO_DEFSTATE_OFF), }; /* Buffalo */ static const struct gpio_led bcm47xx_leds_buffalo_whr2_a54g54[] __initconst = { BCM47XX_GPIO_LED(7, "unk", "diag", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_buffalo_whr_g125[] __initconst = { BCM47XX_GPIO_LED(1, "unk", "bridge", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(2, "unk", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(3, "unk", "internal", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(6, "unk", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(7, "unk", "diag", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_buffalo_whr_g54s[] __initconst = { BCM47XX_GPIO_LED(1, "green", "bridge", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(2, "green", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(3, "green", "internal", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(6, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(7, "red", "diag", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_buffalo_whr_hp_g54[] __initconst = { BCM47XX_GPIO_LED(1, "unk", "bridge", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(2, "unk", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(3, "unk", "internal", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(6, "unk", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(7, "unk", "diag", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_buffalo_wzr_g300n[] __initconst = { BCM47XX_GPIO_LED(1, "unk", "bridge", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(6, "unk", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(7, "unk", "diag", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_buffalo_wzr_rs_g54[] __initconst = { BCM47XX_GPIO_LED(6, "unk", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "unk", "vpn", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(7, "unk", "diag", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_buffalo_wzr_rs_g54hp[] __initconst = { BCM47XX_GPIO_LED(6, "unk", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "unk", "vpn", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(7, "unk", "diag", 1, LEDS_GPIO_DEFSTATE_OFF), }; /* Dell */ static const struct gpio_led bcm47xx_leds_dell_tm2300[] __initconst = { BCM47XX_GPIO_LED(6, "unk", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(7, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), }; /* D-Link */ static const struct gpio_led bcm47xx_leds_dlink_dir130[] __initconst = { BCM47XX_GPIO_LED_TRIGGER(0, "green", "status", 1, "timer"), /* Originally blinking when device is ready, separated from "power" LED */ BCM47XX_GPIO_LED(6, "blue", "unk", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_dlink_dir330[] __initconst = { BCM47XX_GPIO_LED_TRIGGER(0, "green", "status", 1, "timer"), /* Originally blinking when device is ready, separated from "power" LED */ BCM47XX_GPIO_LED(4, "unk", "usb", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(6, "blue", "unk", 1, LEDS_GPIO_DEFSTATE_OFF), }; /* Huawei */ static const struct gpio_led bcm47xx_leds_huawei_b593u_12[] __initconst = { BCM47XX_GPIO_LED(5, "blue", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_huawei_e970[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), }; /* Linksys */ static const struct gpio_led bcm47xx_leds_linksys_e1000v1[] __initconst = { BCM47XX_GPIO_LED(0, "blue", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "blue", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(2, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(4, "blue", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_e1000v21[] __initconst = { BCM47XX_GPIO_LED(5, "blue", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(6, "blue", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(7, "amber", "wps", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(8, "blue", "wps", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_e2000v1[] __initconst = { BCM47XX_GPIO_LED(1, "blue", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(2, "blue", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(3, "blue", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(4, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_e3000v1[] __initconst = { BCM47XX_GPIO_LED(0, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "unk", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(3, "blue", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(5, "unk", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(7, "unk", "usb", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_e3200v1[] __initconst = { BCM47XX_GPIO_LED(3, "green", "power", 1, LEDS_GPIO_DEFSTATE_ON), }; static const struct gpio_led bcm47xx_leds_linksys_e4200v1[] __initconst = { BCM47XX_GPIO_LED(5, "white", "power", 1, LEDS_GPIO_DEFSTATE_ON), }; static const struct gpio_led bcm47xx_leds_linksys_wrt150nv1[] __initconst = { BCM47XX_GPIO_LED(1, "unk", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(3, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(5, "green", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_wrt150nv11[] __initconst = { BCM47XX_GPIO_LED(1, "unk", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(3, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(5, "green", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_wrt160nv1[] __initconst = { BCM47XX_GPIO_LED(1, "unk", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(3, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(5, "blue", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_wrt160nv3[] __initconst = { BCM47XX_GPIO_LED(1, "unk", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(2, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(4, "blue", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_wrt300n_v1[] __initconst = { BCM47XX_GPIO_LED(1, "green", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(3, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(5, "green", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_wrt300nv11[] __initconst = { BCM47XX_GPIO_LED(1, "unk", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(3, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(5, "green", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_wrt310nv1[] __initconst = { BCM47XX_GPIO_LED(1, "blue", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(3, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(9, "blue", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_wrt320n_v1[] __initconst = { BCM47XX_GPIO_LED(1, "blue", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(2, "blue", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(4, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_wrt54g_generic[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "dmz", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "unk", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(5, "white", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(7, "orange", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_wrt54g3gv2[] __initconst = { BCM47XX_GPIO_LED(1, "unk", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(2, "green", "3g", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(3, "blue", "3g", 0, LEDS_GPIO_DEFSTATE_OFF), }; /* Verified on: WRT54GS V1.0 */ static const struct gpio_led bcm47xx_leds_linksys_wrt54g_type_0101[] __initconst = { BCM47XX_GPIO_LED(0, "green", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "green", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(7, "green", "dmz", 1, LEDS_GPIO_DEFSTATE_OFF), }; /* Verified on: WRT54GL V1.1 */ static const struct gpio_led bcm47xx_leds_linksys_wrt54g_type_0467[] __initconst = { BCM47XX_GPIO_LED(0, "green", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "green", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(2, "white", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(3, "orange", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(7, "green", "dmz", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_wrt610nv1[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "usb", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "unk", "power", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(3, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(9, "blue", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_wrt610nv2[] __initconst = { BCM47XX_GPIO_LED(0, "amber", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "unk", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(3, "blue", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(5, "unk", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(7, "unk", "usb", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_linksys_wrtsl54gs[] __initconst = { BCM47XX_GPIO_LED(0, "green", "dmz", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "green", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(5, "white", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(7, "orange", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), }; /* Luxul */ static const struct gpio_led bcm47xx_leds_luxul_abr_4400_v1[] __initconst = { BCM47XX_GPIO_LED(12, "green", "usb", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED_TRIGGER(15, "green", "status", 0, "timer"), }; static const struct gpio_led bcm47xx_leds_luxul_xap_310_v1[] __initconst = { BCM47XX_GPIO_LED_TRIGGER(6, "green", "status", 1, "timer"), }; static const struct gpio_led bcm47xx_leds_luxul_xap_1210_v1[] __initconst = { BCM47XX_GPIO_LED_TRIGGER(6, "green", "status", 1, "timer"), }; static const struct gpio_led bcm47xx_leds_luxul_xap_1230_v1[] __initconst = { BCM47XX_GPIO_LED(3, "blue", "2ghz", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(4, "green", "bridge", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED_TRIGGER(6, "green", "status", 1, "timer"), }; static const struct gpio_led bcm47xx_leds_luxul_xap_1240_v1[] __initconst = { BCM47XX_GPIO_LED(3, "blue", "2ghz", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(4, "green", "bridge", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED_TRIGGER(6, "green", "status", 1, "timer"), }; static const struct gpio_led bcm47xx_leds_luxul_xap_1500_v1[] __initconst = { BCM47XX_GPIO_LED_TRIGGER(13, "green", "status", 1, "timer"), }; static const struct gpio_led bcm47xx_leds_luxul_xap1500_v1_extra[] __initconst = { BCM47XX_GPIO_LED(44, "green", "5ghz", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(76, "green", "2ghz", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_luxul_xbr_4400_v1[] __initconst = { BCM47XX_GPIO_LED(12, "green", "usb", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED_TRIGGER(15, "green", "status", 0, "timer"), }; static const struct gpio_led bcm47xx_leds_luxul_xvw_p30_v1[] __initconst = { BCM47XX_GPIO_LED_TRIGGER(0, "blue", "status", 1, "timer"), BCM47XX_GPIO_LED(1, "green", "link", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_luxul_xwr_600_v1[] __initconst = { BCM47XX_GPIO_LED(3, "green", "wps", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED_TRIGGER(6, "green", "status", 1, "timer"), BCM47XX_GPIO_LED(9, "green", "usb", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_luxul_xwr_1750_v1[] __initconst = { BCM47XX_GPIO_LED(5, "green", "5ghz", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(12, "green", "usb", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED_TRIGGER(13, "green", "status", 0, "timer"), BCM47XX_GPIO_LED(15, "green", "wps", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_luxul_xwr1750_v1_extra[] __initconst = { BCM47XX_GPIO_LED(76, "green", "2ghz", 0, LEDS_GPIO_DEFSTATE_OFF), }; /* Microsoft */ static const struct gpio_led bcm47xx_leds_microsoft_nm700[] __initconst = { BCM47XX_GPIO_LED(6, "unk", "power", 0, LEDS_GPIO_DEFSTATE_ON), }; /* Motorola */ static const struct gpio_led bcm47xx_leds_motorola_we800g[] __initconst = { BCM47XX_GPIO_LED(1, "amber", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(2, "unk", "unk", 1, LEDS_GPIO_DEFSTATE_OFF), /* There are only 3 LEDs: Power, Wireless and Device (ethernet) */ BCM47XX_GPIO_LED(4, "green", "power", 0, LEDS_GPIO_DEFSTATE_ON), }; static const struct gpio_led bcm47xx_leds_motorola_wr850gp[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "unk", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(6, "unk", "dmz", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(7, "unk", "diag", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_motorola_wr850gv2v3[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "unk", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(7, "unk", "diag", 1, LEDS_GPIO_DEFSTATE_OFF), }; /* Netgear */ static const struct gpio_led bcm47xx_leds_netgear_wndr3400v1[] __initconst = { BCM47XX_GPIO_LED(2, "green", "usb", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(3, "green", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(7, "amber", "power", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_netgear_wndr4500v1[] __initconst = { BCM47XX_GPIO_LED(1, "green", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(2, "green", "power", 1, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(3, "amber", "power", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(8, "green", "usb1", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(9, "green", "2ghz", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(11, "blue", "5ghz", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(14, "green", "usb2", 1, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_netgear_wnr1000_v3[] __initconst = { BCM47XX_GPIO_LED(0, "blue", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "green", "wps", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_netgear_wnr3500lv1[] __initconst = { BCM47XX_GPIO_LED(0, "blue", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "green", "wps", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(2, "green", "wan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(3, "green", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(7, "amber", "power", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_netgear_wnr3500lv2[] __initconst = { BCM47XX_GPIO_LED(0, "blue", "wlan", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(1, "green", "wps", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(3, "green", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(7, "amber", "power", 0, LEDS_GPIO_DEFSTATE_OFF), }; static const struct gpio_led bcm47xx_leds_netgear_wnr834bv2[] __initconst = { BCM47XX_GPIO_LED(2, "green", "power", 0, LEDS_GPIO_DEFSTATE_ON), BCM47XX_GPIO_LED(3, "amber", "power", 0, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(7, "unk", "connected", 0, LEDS_GPIO_DEFSTATE_OFF), }; /* Siemens */ static const struct gpio_led bcm47xx_leds_siemens_se505v2[] __initconst = { BCM47XX_GPIO_LED(0, "unk", "dmz", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(3, "unk", "wlan", 1, LEDS_GPIO_DEFSTATE_OFF), BCM47XX_GPIO_LED(5, "unk", "power", 1, LEDS_GPIO_DEFSTATE_ON), }; /* SimpleTech */ static const struct gpio_led bcm47xx_leds_simpletech_simpleshare[] __initconst = { BCM47XX_GPIO_LED(1, "unk", "status", 1, LEDS_GPIO_DEFSTATE_OFF), /* "Ready" LED */ }; /************************************************** * Init **************************************************/ static struct gpio_led_platform_data bcm47xx_leds_pdata __initdata; #define bcm47xx_set_pdata(dev_leds) do { \ bcm47xx_leds_pdata.leds = dev_leds; \ bcm47xx_leds_pdata.num_leds = ARRAY_SIZE(dev_leds); \ } while (0) static struct gpio_led_platform_data bcm47xx_leds_pdata_extra __initdata = {}; #define bcm47xx_set_pdata_extra(dev_leds) do { \ bcm47xx_leds_pdata_extra.leds = dev_leds; \ bcm47xx_leds_pdata_extra.num_leds = ARRAY_SIZE(dev_leds); \ } while (0) void __init bcm47xx_leds_register(void) { enum bcm47xx_board board = bcm47xx_board_get(); switch (board) { case BCM47XX_BOARD_ASUS_RTN10U: bcm47xx_set_pdata(bcm47xx_leds_asus_rtn10u); break; case BCM47XX_BOARD_ASUS_RTN12: bcm47xx_set_pdata(bcm47xx_leds_asus_rtn12); break; case BCM47XX_BOARD_ASUS_RTN15U: bcm47xx_set_pdata(bcm47xx_leds_asus_rtn15u); break; case BCM47XX_BOARD_ASUS_RTN16: bcm47xx_set_pdata(bcm47xx_leds_asus_rtn16); break; case BCM47XX_BOARD_ASUS_RTN66U: bcm47xx_set_pdata(bcm47xx_leds_asus_rtn66u); break; case BCM47XX_BOARD_ASUS_WL300G: bcm47xx_set_pdata(bcm47xx_leds_asus_wl300g); break; case BCM47XX_BOARD_ASUS_WL320GE: bcm47xx_set_pdata(bcm47xx_leds_asus_wl320ge); break; case BCM47XX_BOARD_ASUS_WL330GE: bcm47xx_set_pdata(bcm47xx_leds_asus_wl330ge); break; case BCM47XX_BOARD_ASUS_WL500G: bcm47xx_set_pdata(bcm47xx_leds_asus_wl500g); break; case BCM47XX_BOARD_ASUS_WL500GD: bcm47xx_set_pdata(bcm47xx_leds_asus_wl500gd); break; case BCM47XX_BOARD_ASUS_WL500GPV1: bcm47xx_set_pdata(bcm47xx_leds_asus_wl500gpv1); break; case BCM47XX_BOARD_ASUS_WL500GPV2: bcm47xx_set_pdata(bcm47xx_leds_asus_wl500gpv2); break; case BCM47XX_BOARD_ASUS_WL500W: bcm47xx_set_pdata(bcm47xx_leds_asus_wl500w); break; case BCM47XX_BOARD_ASUS_WL520GC: bcm47xx_set_pdata(bcm47xx_leds_asus_wl520gc); break; case BCM47XX_BOARD_ASUS_WL520GU: bcm47xx_set_pdata(bcm47xx_leds_asus_wl520gu); break; case BCM47XX_BOARD_ASUS_WL700GE: bcm47xx_set_pdata(bcm47xx_leds_asus_wl700ge); break; case BCM47XX_BOARD_ASUS_WLHDD: bcm47xx_set_pdata(bcm47xx_leds_asus_wlhdd); break; case BCM47XX_BOARD_BELKIN_F7D3301: case BCM47XX_BOARD_BELKIN_F7D3302: case BCM47XX_BOARD_BELKIN_F7D4301: case BCM47XX_BOARD_BELKIN_F7D4302: case BCM47XX_BOARD_BELKIN_F7D4401: bcm47xx_set_pdata(bcm47xx_leds_belkin_f7d4301); break; case BCM47XX_BOARD_BUFFALO_WHR2_A54G54: bcm47xx_set_pdata(bcm47xx_leds_buffalo_whr2_a54g54); break; case BCM47XX_BOARD_BUFFALO_WHR_G125: bcm47xx_set_pdata(bcm47xx_leds_buffalo_whr_g125); break; case BCM47XX_BOARD_BUFFALO_WHR_G54S: bcm47xx_set_pdata(bcm47xx_leds_buffalo_whr_g54s); break; case BCM47XX_BOARD_BUFFALO_WHR_HP_G54: bcm47xx_set_pdata(bcm47xx_leds_buffalo_whr_hp_g54); break; case BCM47XX_BOARD_BUFFALO_WZR_G300N: bcm47xx_set_pdata(bcm47xx_leds_buffalo_wzr_g300n); break; case BCM47XX_BOARD_BUFFALO_WZR_RS_G54: bcm47xx_set_pdata(bcm47xx_leds_buffalo_wzr_rs_g54); break; case BCM47XX_BOARD_BUFFALO_WZR_RS_G54HP: bcm47xx_set_pdata(bcm47xx_leds_buffalo_wzr_rs_g54hp); break; case BCM47XX_BOARD_DELL_TM2300: bcm47xx_set_pdata(bcm47xx_leds_dell_tm2300); break; case BCM47XX_BOARD_DLINK_DIR130: bcm47xx_set_pdata(bcm47xx_leds_dlink_dir130); break; case BCM47XX_BOARD_DLINK_DIR330: bcm47xx_set_pdata(bcm47xx_leds_dlink_dir330); break; case BCM47XX_BOARD_HUAWEI_B593U_12: bcm47xx_set_pdata(bcm47xx_leds_huawei_b593u_12); break; case BCM47XX_BOARD_HUAWEI_E970: bcm47xx_set_pdata(bcm47xx_leds_huawei_e970); break; case BCM47XX_BOARD_LINKSYS_E1000V1: bcm47xx_set_pdata(bcm47xx_leds_linksys_e1000v1); break; case BCM47XX_BOARD_LINKSYS_E1000V21: bcm47xx_set_pdata(bcm47xx_leds_linksys_e1000v21); break; case BCM47XX_BOARD_LINKSYS_E2000V1: bcm47xx_set_pdata(bcm47xx_leds_linksys_e2000v1); break; case BCM47XX_BOARD_LINKSYS_E3000V1: bcm47xx_set_pdata(bcm47xx_leds_linksys_e3000v1); break; case BCM47XX_BOARD_LINKSYS_E3200V1: bcm47xx_set_pdata(bcm47xx_leds_linksys_e3200v1); break; case BCM47XX_BOARD_LINKSYS_E4200V1: bcm47xx_set_pdata(bcm47xx_leds_linksys_e4200v1); break; case BCM47XX_BOARD_LINKSYS_WRT150NV1: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt150nv1); break; case BCM47XX_BOARD_LINKSYS_WRT150NV11: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt150nv11); break; case BCM47XX_BOARD_LINKSYS_WRT160NV1: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt160nv1); break; case BCM47XX_BOARD_LINKSYS_WRT160NV3: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt160nv3); break; case BCM47XX_BOARD_LINKSYS_WRT300N_V1: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt300n_v1); break; case BCM47XX_BOARD_LINKSYS_WRT300NV11: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt300nv11); break; case BCM47XX_BOARD_LINKSYS_WRT310NV1: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt310nv1); break; case BCM47XX_BOARD_LINKSYS_WRT320N_V1: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt320n_v1); break; case BCM47XX_BOARD_LINKSYS_WRT54G3GV2: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt54g3gv2); break; case BCM47XX_BOARD_LINKSYS_WRT54G_TYPE_0101: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt54g_type_0101); break; case BCM47XX_BOARD_LINKSYS_WRT54G_TYPE_0467: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt54g_type_0467); break; case BCM47XX_BOARD_LINKSYS_WRT54G_TYPE_0708: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt54g_generic); break; case BCM47XX_BOARD_LINKSYS_WRT610NV1: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt610nv1); break; case BCM47XX_BOARD_LINKSYS_WRT610NV2: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrt610nv2); break; case BCM47XX_BOARD_LINKSYS_WRTSL54GS: bcm47xx_set_pdata(bcm47xx_leds_linksys_wrtsl54gs); break; case BCM47XX_BOARD_LUXUL_ABR_4400_V1: bcm47xx_set_pdata(bcm47xx_leds_luxul_abr_4400_v1); break; case BCM47XX_BOARD_LUXUL_XAP_310_V1: bcm47xx_set_pdata(bcm47xx_leds_luxul_xap_310_v1); break; case BCM47XX_BOARD_LUXUL_XAP_1210_V1: bcm47xx_set_pdata(bcm47xx_leds_luxul_xap_1210_v1); break; case BCM47XX_BOARD_LUXUL_XAP_1230_V1: bcm47xx_set_pdata(bcm47xx_leds_luxul_xap_1230_v1); break; case BCM47XX_BOARD_LUXUL_XAP_1240_V1: bcm47xx_set_pdata(bcm47xx_leds_luxul_xap_1240_v1); break; case BCM47XX_BOARD_LUXUL_XAP_1500_V1: bcm47xx_set_pdata(bcm47xx_leds_luxul_xap_1500_v1); bcm47xx_set_pdata_extra(bcm47xx_leds_luxul_xap1500_v1_extra); break; case BCM47XX_BOARD_LUXUL_XBR_4400_V1: bcm47xx_set_pdata(bcm47xx_leds_luxul_xbr_4400_v1); break; case BCM47XX_BOARD_LUXUL_XVW_P30_V1: bcm47xx_set_pdata(bcm47xx_leds_luxul_xvw_p30_v1); break; case BCM47XX_BOARD_LUXUL_XWR_600_V1: bcm47xx_set_pdata(bcm47xx_leds_luxul_xwr_600_v1); break; case BCM47XX_BOARD_LUXUL_XWR_1750_V1: bcm47xx_set_pdata(bcm47xx_leds_luxul_xwr_1750_v1); bcm47xx_set_pdata_extra(bcm47xx_leds_luxul_xwr1750_v1_extra); break; case BCM47XX_BOARD_MICROSOFT_MN700: bcm47xx_set_pdata(bcm47xx_leds_microsoft_nm700); break; case BCM47XX_BOARD_MOTOROLA_WE800G: bcm47xx_set_pdata(bcm47xx_leds_motorola_we800g); break; case BCM47XX_BOARD_MOTOROLA_WR850GP: bcm47xx_set_pdata(bcm47xx_leds_motorola_wr850gp); break; case BCM47XX_BOARD_MOTOROLA_WR850GV2V3: bcm47xx_set_pdata(bcm47xx_leds_motorola_wr850gv2v3); break; case BCM47XX_BOARD_NETGEAR_WNDR3400V1: bcm47xx_set_pdata(bcm47xx_leds_netgear_wndr3400v1); break; case BCM47XX_BOARD_NETGEAR_WNDR4500V1: bcm47xx_set_pdata(bcm47xx_leds_netgear_wndr4500v1); break; case BCM47XX_BOARD_NETGEAR_WNR1000_V3: bcm47xx_set_pdata(bcm47xx_leds_netgear_wnr1000_v3); break; case BCM47XX_BOARD_NETGEAR_WNR3500L: bcm47xx_set_pdata(bcm47xx_leds_netgear_wnr3500lv1); break; case BCM47XX_BOARD_NETGEAR_WNR3500L_V2: bcm47xx_set_pdata(bcm47xx_leds_netgear_wnr3500lv2); break; case BCM47XX_BOARD_NETGEAR_WNR834BV2: bcm47xx_set_pdata(bcm47xx_leds_netgear_wnr834bv2); break; case BCM47XX_BOARD_SIEMENS_SE505V2: bcm47xx_set_pdata(bcm47xx_leds_siemens_se505v2); break; case BCM47XX_BOARD_SIMPLETECH_SIMPLESHARE: bcm47xx_set_pdata(bcm47xx_leds_simpletech_simpleshare); break; default: pr_debug("No LEDs configuration found for this device\n"); return; } gpio_led_register_device(-1, &bcm47xx_leds_pdata); if (bcm47xx_leds_pdata_extra.num_leds) gpio_led_register_device(0, &bcm47xx_leds_pdata_extra); }
linux-master
arch/mips/bcm47xx/leds.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2007 Felix Fietkau <[email protected]> * Copyright (C) 2007 Eugene Konev <[email protected]> */ #include <linux/memblock.h> #include <linux/init.h> #include <linux/mm.h> #include <linux/pfn.h> #include <linux/proc_fs.h> #include <linux/string.h> #include <linux/swap.h> #include <asm/bootinfo.h> #include <asm/page.h> #include <asm/sections.h> #include <asm/mach-ar7/ar7.h> static int __init memsize(void) { u32 size = (64 << 20); u32 *addr = (u32 *)KSEG1ADDR(AR7_SDRAM_BASE + size - 4); u32 *kernel_end = (u32 *)KSEG1ADDR(CPHYSADDR((u32)&_end)); u32 *tmpaddr = addr; while (tmpaddr > kernel_end) { *tmpaddr = (u32)tmpaddr; size >>= 1; tmpaddr -= size >> 2; } do { tmpaddr += size >> 2; if (*tmpaddr != (u32)tmpaddr) break; size <<= 1; } while (size < (64 << 20)); writel((u32)tmpaddr, &addr); return size; } void __init prom_meminit(void) { unsigned long pages; pages = memsize() >> PAGE_SHIFT; memblock_add(PHYS_OFFSET, pages << PAGE_SHIFT); }
linux-master
arch/mips/ar7/memory.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2006,2007 Felix Fietkau <[email protected]> * Copyright (C) 2006,2007 Eugene Konev <[email protected]> */ #include <linux/interrupt.h> #include <linux/io.h> #include <linux/irq.h> #include <asm/irq_cpu.h> #include <asm/mipsregs.h> #include <asm/mach-ar7/ar7.h> #define EXCEPT_OFFSET 0x80 #define PACE_OFFSET 0xA0 #define CHNLS_OFFSET 0x200 #define REG_OFFSET(irq, reg) ((irq) / 32 * 0x4 + reg * 0x10) #define SEC_REG_OFFSET(reg) (EXCEPT_OFFSET + reg * 0x8) #define SEC_SR_OFFSET (SEC_REG_OFFSET(0)) /* 0x80 */ #define CR_OFFSET(irq) (REG_OFFSET(irq, 1)) /* 0x10 */ #define SEC_CR_OFFSET (SEC_REG_OFFSET(1)) /* 0x88 */ #define ESR_OFFSET(irq) (REG_OFFSET(irq, 2)) /* 0x20 */ #define SEC_ESR_OFFSET (SEC_REG_OFFSET(2)) /* 0x90 */ #define ECR_OFFSET(irq) (REG_OFFSET(irq, 3)) /* 0x30 */ #define SEC_ECR_OFFSET (SEC_REG_OFFSET(3)) /* 0x98 */ #define PIR_OFFSET (0x40) #define MSR_OFFSET (0x44) #define PM_OFFSET(irq) (REG_OFFSET(irq, 5)) /* 0x50 */ #define TM_OFFSET(irq) (REG_OFFSET(irq, 6)) /* 0x60 */ #define REG(addr) ((u32 *)(KSEG1ADDR(AR7_REGS_IRQ) + addr)) #define CHNL_OFFSET(chnl) (CHNLS_OFFSET + (chnl * 4)) static int ar7_irq_base; static void ar7_unmask_irq(struct irq_data *d) { writel(1 << ((d->irq - ar7_irq_base) % 32), REG(ESR_OFFSET(d->irq - ar7_irq_base))); } static void ar7_mask_irq(struct irq_data *d) { writel(1 << ((d->irq - ar7_irq_base) % 32), REG(ECR_OFFSET(d->irq - ar7_irq_base))); } static void ar7_ack_irq(struct irq_data *d) { writel(1 << ((d->irq - ar7_irq_base) % 32), REG(CR_OFFSET(d->irq - ar7_irq_base))); } static void ar7_unmask_sec_irq(struct irq_data *d) { writel(1 << (d->irq - ar7_irq_base - 40), REG(SEC_ESR_OFFSET)); } static void ar7_mask_sec_irq(struct irq_data *d) { writel(1 << (d->irq - ar7_irq_base - 40), REG(SEC_ECR_OFFSET)); } static void ar7_ack_sec_irq(struct irq_data *d) { writel(1 << (d->irq - ar7_irq_base - 40), REG(SEC_CR_OFFSET)); } static struct irq_chip ar7_irq_type = { .name = "AR7", .irq_unmask = ar7_unmask_irq, .irq_mask = ar7_mask_irq, .irq_ack = ar7_ack_irq }; static struct irq_chip ar7_sec_irq_type = { .name = "AR7", .irq_unmask = ar7_unmask_sec_irq, .irq_mask = ar7_mask_sec_irq, .irq_ack = ar7_ack_sec_irq, }; static void __init ar7_irq_init(int base) { int i; /* * Disable interrupts and clear pending */ writel(0xffffffff, REG(ECR_OFFSET(0))); writel(0xff, REG(ECR_OFFSET(32))); writel(0xffffffff, REG(SEC_ECR_OFFSET)); writel(0xffffffff, REG(CR_OFFSET(0))); writel(0xff, REG(CR_OFFSET(32))); writel(0xffffffff, REG(SEC_CR_OFFSET)); ar7_irq_base = base; for (i = 0; i < 40; i++) { writel(i, REG(CHNL_OFFSET(i))); /* Primary IRQ's */ irq_set_chip_and_handler(base + i, &ar7_irq_type, handle_level_irq); /* Secondary IRQ's */ if (i < 32) irq_set_chip_and_handler(base + i + 40, &ar7_sec_irq_type, handle_level_irq); } if (request_irq(2, no_action, IRQF_NO_THREAD, "AR7 cascade interrupt", NULL)) pr_err("Failed to request irq 2 (AR7 cascade interrupt)\n"); if (request_irq(ar7_irq_base, no_action, IRQF_NO_THREAD, "AR7 cascade interrupt", NULL)) { pr_err("Failed to request irq %d (AR7 cascade interrupt)\n", ar7_irq_base); } set_c0_status(IE_IRQ0); } void __init arch_init_irq(void) { mips_cpu_irq_init(); ar7_irq_init(8); } static void ar7_cascade(void) { u32 status; int i, irq; /* Primary IRQ's */ irq = readl(REG(PIR_OFFSET)) & 0x3f; if (irq) { do_IRQ(ar7_irq_base + irq); return; } /* Secondary IRQ's are cascaded through primary '0' */ writel(1, REG(CR_OFFSET(irq))); status = readl(REG(SEC_SR_OFFSET)); for (i = 0; i < 32; i++) { if (status & 1) { do_IRQ(ar7_irq_base + i + 40); return; } status >>= 1; } spurious_interrupt(); } asmlinkage void plat_irq_dispatch(void) { unsigned int pending = read_c0_status() & read_c0_cause() & ST0_IM; if (pending & STATUSF_IP7) /* cpu timer */ do_IRQ(7); else if (pending & STATUSF_IP2) /* int0 hardware line */ ar7_cascade(); else spurious_interrupt(); }
linux-master
arch/mips/ar7/irq.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2007 Felix Fietkau <[email protected]> * Copyright (C) 2007 Eugene Konev <[email protected]> * Copyright (C) 2009-2010 Florian Fainelli <[email protected]> */ #include <linux/init.h> #include <linux/export.h> #include <linux/gpio/driver.h> #include <asm/mach-ar7/ar7.h> #define AR7_GPIO_MAX 32 #define TITAN_GPIO_MAX 51 struct ar7_gpio_chip { void __iomem *regs; struct gpio_chip chip; }; static int ar7_gpio_get_value(struct gpio_chip *chip, unsigned gpio) { struct ar7_gpio_chip *gpch = gpiochip_get_data(chip); void __iomem *gpio_in = gpch->regs + AR7_GPIO_INPUT; return !!(readl(gpio_in) & (1 << gpio)); } static int titan_gpio_get_value(struct gpio_chip *chip, unsigned gpio) { struct ar7_gpio_chip *gpch = gpiochip_get_data(chip); void __iomem *gpio_in0 = gpch->regs + TITAN_GPIO_INPUT_0; void __iomem *gpio_in1 = gpch->regs + TITAN_GPIO_INPUT_1; return readl(gpio >> 5 ? gpio_in1 : gpio_in0) & (1 << (gpio & 0x1f)); } static void ar7_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value) { struct ar7_gpio_chip *gpch = gpiochip_get_data(chip); void __iomem *gpio_out = gpch->regs + AR7_GPIO_OUTPUT; unsigned tmp; tmp = readl(gpio_out) & ~(1 << gpio); if (value) tmp |= 1 << gpio; writel(tmp, gpio_out); } static void titan_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value) { struct ar7_gpio_chip *gpch = gpiochip_get_data(chip); void __iomem *gpio_out0 = gpch->regs + TITAN_GPIO_OUTPUT_0; void __iomem *gpio_out1 = gpch->regs + TITAN_GPIO_OUTPUT_1; unsigned tmp; tmp = readl(gpio >> 5 ? gpio_out1 : gpio_out0) & ~(1 << (gpio & 0x1f)); if (value) tmp |= 1 << (gpio & 0x1f); writel(tmp, gpio >> 5 ? gpio_out1 : gpio_out0); } static int ar7_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) { struct ar7_gpio_chip *gpch = gpiochip_get_data(chip); void __iomem *gpio_dir = gpch->regs + AR7_GPIO_DIR; writel(readl(gpio_dir) | (1 << gpio), gpio_dir); return 0; } static int titan_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) { struct ar7_gpio_chip *gpch = gpiochip_get_data(chip); void __iomem *gpio_dir0 = gpch->regs + TITAN_GPIO_DIR_0; void __iomem *gpio_dir1 = gpch->regs + TITAN_GPIO_DIR_1; if (gpio >= TITAN_GPIO_MAX) return -EINVAL; writel(readl(gpio >> 5 ? gpio_dir1 : gpio_dir0) | (1 << (gpio & 0x1f)), gpio >> 5 ? gpio_dir1 : gpio_dir0); return 0; } static int ar7_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, int value) { struct ar7_gpio_chip *gpch = gpiochip_get_data(chip); void __iomem *gpio_dir = gpch->regs + AR7_GPIO_DIR; ar7_gpio_set_value(chip, gpio, value); writel(readl(gpio_dir) & ~(1 << gpio), gpio_dir); return 0; } static int titan_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, int value) { struct ar7_gpio_chip *gpch = gpiochip_get_data(chip); void __iomem *gpio_dir0 = gpch->regs + TITAN_GPIO_DIR_0; void __iomem *gpio_dir1 = gpch->regs + TITAN_GPIO_DIR_1; if (gpio >= TITAN_GPIO_MAX) return -EINVAL; titan_gpio_set_value(chip, gpio, value); writel(readl(gpio >> 5 ? gpio_dir1 : gpio_dir0) & ~(1 << (gpio & 0x1f)), gpio >> 5 ? gpio_dir1 : gpio_dir0); return 0; } static struct ar7_gpio_chip ar7_gpio_chip = { .chip = { .label = "ar7-gpio", .direction_input = ar7_gpio_direction_input, .direction_output = ar7_gpio_direction_output, .set = ar7_gpio_set_value, .get = ar7_gpio_get_value, .base = 0, .ngpio = AR7_GPIO_MAX, } }; static struct ar7_gpio_chip titan_gpio_chip = { .chip = { .label = "titan-gpio", .direction_input = titan_gpio_direction_input, .direction_output = titan_gpio_direction_output, .set = titan_gpio_set_value, .get = titan_gpio_get_value, .base = 0, .ngpio = TITAN_GPIO_MAX, } }; static inline int ar7_gpio_enable_ar7(unsigned gpio) { void __iomem *gpio_en = ar7_gpio_chip.regs + AR7_GPIO_ENABLE; writel(readl(gpio_en) | (1 << gpio), gpio_en); return 0; } static inline int ar7_gpio_enable_titan(unsigned gpio) { void __iomem *gpio_en0 = titan_gpio_chip.regs + TITAN_GPIO_ENBL_0; void __iomem *gpio_en1 = titan_gpio_chip.regs + TITAN_GPIO_ENBL_1; writel(readl(gpio >> 5 ? gpio_en1 : gpio_en0) | (1 << (gpio & 0x1f)), gpio >> 5 ? gpio_en1 : gpio_en0); return 0; } int ar7_gpio_enable(unsigned gpio) { return ar7_is_titan() ? ar7_gpio_enable_titan(gpio) : ar7_gpio_enable_ar7(gpio); } EXPORT_SYMBOL(ar7_gpio_enable); static inline int ar7_gpio_disable_ar7(unsigned gpio) { void __iomem *gpio_en = ar7_gpio_chip.regs + AR7_GPIO_ENABLE; writel(readl(gpio_en) & ~(1 << gpio), gpio_en); return 0; } static inline int ar7_gpio_disable_titan(unsigned gpio) { void __iomem *gpio_en0 = titan_gpio_chip.regs + TITAN_GPIO_ENBL_0; void __iomem *gpio_en1 = titan_gpio_chip.regs + TITAN_GPIO_ENBL_1; writel(readl(gpio >> 5 ? gpio_en1 : gpio_en0) & ~(1 << (gpio & 0x1f)), gpio >> 5 ? gpio_en1 : gpio_en0); return 0; } int ar7_gpio_disable(unsigned gpio) { return ar7_is_titan() ? ar7_gpio_disable_titan(gpio) : ar7_gpio_disable_ar7(gpio); } EXPORT_SYMBOL(ar7_gpio_disable); struct titan_gpio_cfg { u32 reg; u32 shift; u32 func; }; static const struct titan_gpio_cfg titan_gpio_table[] = { /* reg, start bit, mux value */ {4, 24, 1}, {4, 26, 1}, {4, 28, 1}, {4, 30, 1}, {5, 6, 1}, {5, 8, 1}, {5, 10, 1}, {5, 12, 1}, {7, 14, 3}, {7, 16, 3}, {7, 18, 3}, {7, 20, 3}, {7, 22, 3}, {7, 26, 3}, {7, 28, 3}, {7, 30, 3}, {8, 0, 3}, {8, 2, 3}, {8, 4, 3}, {8, 10, 3}, {8, 14, 3}, {8, 16, 3}, {8, 18, 3}, {8, 20, 3}, {9, 8, 3}, {9, 10, 3}, {9, 12, 3}, {9, 14, 3}, {9, 18, 3}, {9, 20, 3}, {9, 24, 3}, {9, 26, 3}, {9, 28, 3}, {9, 30, 3}, {10, 0, 3}, {10, 2, 3}, {10, 8, 3}, {10, 10, 3}, {10, 12, 3}, {10, 14, 3}, {13, 12, 3}, {13, 14, 3}, {13, 16, 3}, {13, 18, 3}, {13, 24, 3}, {13, 26, 3}, {13, 28, 3}, {13, 30, 3}, {14, 2, 3}, {14, 6, 3}, {14, 8, 3}, {14, 12, 3} }; static int titan_gpio_pinsel(unsigned gpio) { struct titan_gpio_cfg gpio_cfg; u32 mux_status, pin_sel_reg, tmp; void __iomem *pin_sel = (void __iomem *)KSEG1ADDR(AR7_REGS_PINSEL); if (gpio >= ARRAY_SIZE(titan_gpio_table)) return -EINVAL; gpio_cfg = titan_gpio_table[gpio]; pin_sel_reg = gpio_cfg.reg - 1; mux_status = (readl(pin_sel + pin_sel_reg) >> gpio_cfg.shift) & 0x3; /* Check the mux status */ if (!((mux_status == 0) || (mux_status == gpio_cfg.func))) return 0; /* Set the pin sel value */ tmp = readl(pin_sel + pin_sel_reg); tmp |= ((gpio_cfg.func & 0x3) << gpio_cfg.shift); writel(tmp, pin_sel + pin_sel_reg); return 0; } /* Perform minimal Titan GPIO configuration */ static void titan_gpio_init(void) { unsigned i; for (i = 44; i < 48; i++) { titan_gpio_pinsel(i); ar7_gpio_enable_titan(i); titan_gpio_direction_input(&titan_gpio_chip.chip, i); } } int __init ar7_gpio_init(void) { int ret; struct ar7_gpio_chip *gpch; unsigned size; if (!ar7_is_titan()) { gpch = &ar7_gpio_chip; size = 0x10; } else { gpch = &titan_gpio_chip; size = 0x1f; } gpch->regs = ioremap(AR7_REGS_GPIO, size); if (!gpch->regs) { printk(KERN_ERR "%s: failed to ioremap regs\n", gpch->chip.label); return -ENOMEM; } ret = gpiochip_add_data(&gpch->chip, gpch); if (ret) { printk(KERN_ERR "%s: failed to add gpiochip\n", gpch->chip.label); iounmap(gpch->regs); return ret; } printk(KERN_INFO "%s: registered %d GPIOs\n", gpch->chip.label, gpch->chip.ngpio); if (ar7_is_titan()) titan_gpio_init(); return ret; }
linux-master
arch/mips/ar7/gpio.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2006,2007 Felix Fietkau <[email protected]> * Copyright (C) 2006,2007 Eugene Konev <[email protected]> */ #include <linux/init.h> #include <linux/types.h> #include <linux/delay.h> #include <linux/dma-mapping.h> #include <linux/platform_device.h> #include <linux/mtd/physmap.h> #include <linux/serial.h> #include <linux/serial_8250.h> #include <linux/ioport.h> #include <linux/io.h> #include <linux/vlynq.h> #include <linux/leds.h> #include <linux/string.h> #include <linux/etherdevice.h> #include <linux/phy.h> #include <linux/phy_fixed.h> #include <linux/gpio.h> #include <linux/clk.h> #include <asm/addrspace.h> #include <asm/mach-ar7/ar7.h> #include <asm/mach-ar7/prom.h> /***************************************************************************** * VLYNQ Bus ****************************************************************************/ struct plat_vlynq_data { struct plat_vlynq_ops ops; int gpio_bit; int reset_bit; }; static int vlynq_on(struct vlynq_device *dev) { int ret; struct plat_vlynq_data *pdata = dev->dev.platform_data; ret = gpio_request(pdata->gpio_bit, "vlynq"); if (ret) goto out; ar7_device_reset(pdata->reset_bit); ret = ar7_gpio_disable(pdata->gpio_bit); if (ret) goto out_enabled; ret = ar7_gpio_enable(pdata->gpio_bit); if (ret) goto out_enabled; ret = gpio_direction_output(pdata->gpio_bit, 0); if (ret) goto out_gpio_enabled; msleep(50); gpio_set_value(pdata->gpio_bit, 1); msleep(50); return 0; out_gpio_enabled: ar7_gpio_disable(pdata->gpio_bit); out_enabled: ar7_device_disable(pdata->reset_bit); gpio_free(pdata->gpio_bit); out: return ret; } static void vlynq_off(struct vlynq_device *dev) { struct plat_vlynq_data *pdata = dev->dev.platform_data; ar7_gpio_disable(pdata->gpio_bit); gpio_free(pdata->gpio_bit); ar7_device_disable(pdata->reset_bit); } static struct resource vlynq_low_res[] = { { .name = "regs", .flags = IORESOURCE_MEM, .start = AR7_REGS_VLYNQ0, .end = AR7_REGS_VLYNQ0 + 0xff, }, { .name = "irq", .flags = IORESOURCE_IRQ, .start = 29, .end = 29, }, { .name = "mem", .flags = IORESOURCE_MEM, .start = 0x04000000, .end = 0x04ffffff, }, { .name = "devirq", .flags = IORESOURCE_IRQ, .start = 80, .end = 111, }, }; static struct resource vlynq_high_res[] = { { .name = "regs", .flags = IORESOURCE_MEM, .start = AR7_REGS_VLYNQ1, .end = AR7_REGS_VLYNQ1 + 0xff, }, { .name = "irq", .flags = IORESOURCE_IRQ, .start = 33, .end = 33, }, { .name = "mem", .flags = IORESOURCE_MEM, .start = 0x0c000000, .end = 0x0cffffff, }, { .name = "devirq", .flags = IORESOURCE_IRQ, .start = 112, .end = 143, }, }; static struct plat_vlynq_data vlynq_low_data = { .ops = { .on = vlynq_on, .off = vlynq_off, }, .reset_bit = 20, .gpio_bit = 18, }; static struct plat_vlynq_data vlynq_high_data = { .ops = { .on = vlynq_on, .off = vlynq_off, }, .reset_bit = 16, .gpio_bit = 19, }; static struct platform_device vlynq_low = { .id = 0, .name = "vlynq", .dev = { .platform_data = &vlynq_low_data, }, .resource = vlynq_low_res, .num_resources = ARRAY_SIZE(vlynq_low_res), }; static struct platform_device vlynq_high = { .id = 1, .name = "vlynq", .dev = { .platform_data = &vlynq_high_data, }, .resource = vlynq_high_res, .num_resources = ARRAY_SIZE(vlynq_high_res), }; /***************************************************************************** * Flash ****************************************************************************/ static struct resource physmap_flash_resource = { .name = "mem", .flags = IORESOURCE_MEM, .start = 0x10000000, .end = 0x107fffff, }; static const char *ar7_probe_types[] = { "ar7part", NULL }; static struct physmap_flash_data physmap_flash_data = { .width = 2, .part_probe_types = ar7_probe_types, }; static struct platform_device physmap_flash = { .name = "physmap-flash", .dev = { .platform_data = &physmap_flash_data, }, .resource = &physmap_flash_resource, .num_resources = 1, }; /***************************************************************************** * Ethernet ****************************************************************************/ static struct resource cpmac_low_res[] = { { .name = "regs", .flags = IORESOURCE_MEM, .start = AR7_REGS_MAC0, .end = AR7_REGS_MAC0 + 0x7ff, }, { .name = "irq", .flags = IORESOURCE_IRQ, .start = 27, .end = 27, }, }; static struct resource cpmac_high_res[] = { { .name = "regs", .flags = IORESOURCE_MEM, .start = AR7_REGS_MAC1, .end = AR7_REGS_MAC1 + 0x7ff, }, { .name = "irq", .flags = IORESOURCE_IRQ, .start = 41, .end = 41, }, }; static struct fixed_phy_status fixed_phy_status __initdata = { .link = 1, .speed = 100, .duplex = 1, }; static struct plat_cpmac_data cpmac_low_data = { .reset_bit = 17, .power_bit = 20, .phy_mask = 0x80000000, }; static struct plat_cpmac_data cpmac_high_data = { .reset_bit = 21, .power_bit = 22, .phy_mask = 0x7fffffff, }; static u64 cpmac_dma_mask = DMA_BIT_MASK(32); static struct platform_device cpmac_low = { .id = 0, .name = "cpmac", .dev = { .dma_mask = &cpmac_dma_mask, .coherent_dma_mask = DMA_BIT_MASK(32), .platform_data = &cpmac_low_data, }, .resource = cpmac_low_res, .num_resources = ARRAY_SIZE(cpmac_low_res), }; static struct platform_device cpmac_high = { .id = 1, .name = "cpmac", .dev = { .dma_mask = &cpmac_dma_mask, .coherent_dma_mask = DMA_BIT_MASK(32), .platform_data = &cpmac_high_data, }, .resource = cpmac_high_res, .num_resources = ARRAY_SIZE(cpmac_high_res), }; static void __init cpmac_get_mac(int instance, unsigned char *dev_addr) { char name[5], *mac; sprintf(name, "mac%c", 'a' + instance); mac = prom_getenv(name); if (!mac && instance) { sprintf(name, "mac%c", 'a'); mac = prom_getenv(name); } if (mac) { if (!mac_pton(mac, dev_addr)) { pr_warn("cannot parse mac address, using random address\n"); eth_random_addr(dev_addr); } } else eth_random_addr(dev_addr); } /***************************************************************************** * USB ****************************************************************************/ static struct resource usb_res[] = { { .name = "regs", .flags = IORESOURCE_MEM, .start = AR7_REGS_USB, .end = AR7_REGS_USB + 0xff, }, { .name = "irq", .flags = IORESOURCE_IRQ, .start = 32, .end = 32, }, { .name = "mem", .flags = IORESOURCE_MEM, .start = 0x03400000, .end = 0x03401fff, }, }; static struct platform_device ar7_udc = { .name = "ar7_udc", .resource = usb_res, .num_resources = ARRAY_SIZE(usb_res), }; /***************************************************************************** * LEDs ****************************************************************************/ static const struct gpio_led default_leds[] = { { .name = "status", .gpio = 8, .active_low = 1, }, }; static const struct gpio_led titan_leds[] = { { .name = "status", .gpio = 8, .active_low = 1, }, { .name = "wifi", .gpio = 13, .active_low = 1, }, }; static const struct gpio_led dsl502t_leds[] = { { .name = "status", .gpio = 9, .active_low = 1, }, { .name = "ethernet", .gpio = 7, .active_low = 1, }, { .name = "usb", .gpio = 12, .active_low = 1, }, }; static const struct gpio_led dg834g_leds[] = { { .name = "ppp", .gpio = 6, .active_low = 1, }, { .name = "status", .gpio = 7, .active_low = 1, }, { .name = "adsl", .gpio = 8, .active_low = 1, }, { .name = "wifi", .gpio = 12, .active_low = 1, }, { .name = "power", .gpio = 14, .active_low = 1, .default_trigger = "default-on", }, }; static const struct gpio_led fb_sl_leds[] = { { .name = "1", .gpio = 7, }, { .name = "2", .gpio = 13, .active_low = 1, }, { .name = "3", .gpio = 10, .active_low = 1, }, { .name = "4", .gpio = 12, .active_low = 1, }, { .name = "5", .gpio = 9, .active_low = 1, }, }; static const struct gpio_led fb_fon_leds[] = { { .name = "1", .gpio = 8, }, { .name = "2", .gpio = 3, .active_low = 1, }, { .name = "3", .gpio = 5, }, { .name = "4", .gpio = 4, .active_low = 1, }, { .name = "5", .gpio = 11, .active_low = 1, }, }; static const struct gpio_led gt701_leds[] = { { .name = "inet:green", .gpio = 13, .active_low = 1, }, { .name = "usb", .gpio = 12, .active_low = 1, }, { .name = "inet:red", .gpio = 9, .active_low = 1, }, { .name = "power:red", .gpio = 7, .active_low = 1, }, { .name = "power:green", .gpio = 8, .active_low = 1, .default_trigger = "default-on", }, { .name = "ethernet", .gpio = 10, .active_low = 1, }, }; static struct gpio_led_platform_data ar7_led_data; static struct platform_device ar7_gpio_leds = { .name = "leds-gpio", .dev = { .platform_data = &ar7_led_data, } }; static void __init detect_leds(void) { char *prid, *usb_prod; /* Default LEDs */ ar7_led_data.num_leds = ARRAY_SIZE(default_leds); ar7_led_data.leds = default_leds; /* FIXME: the whole thing is unreliable */ prid = prom_getenv("ProductID"); usb_prod = prom_getenv("usb_prod"); /* If we can't get the product id from PROM, use the default LEDs */ if (!prid) return; if (strstr(prid, "Fritz_Box_FON")) { ar7_led_data.num_leds = ARRAY_SIZE(fb_fon_leds); ar7_led_data.leds = fb_fon_leds; } else if (strstr(prid, "Fritz_Box_")) { ar7_led_data.num_leds = ARRAY_SIZE(fb_sl_leds); ar7_led_data.leds = fb_sl_leds; } else if ((!strcmp(prid, "AR7RD") || !strcmp(prid, "AR7DB")) && usb_prod != NULL && strstr(usb_prod, "DSL-502T")) { ar7_led_data.num_leds = ARRAY_SIZE(dsl502t_leds); ar7_led_data.leds = dsl502t_leds; } else if (strstr(prid, "DG834")) { ar7_led_data.num_leds = ARRAY_SIZE(dg834g_leds); ar7_led_data.leds = dg834g_leds; } else if (strstr(prid, "CYWM") || strstr(prid, "CYWL")) { ar7_led_data.num_leds = ARRAY_SIZE(titan_leds); ar7_led_data.leds = titan_leds; } else if (strstr(prid, "GT701")) { ar7_led_data.num_leds = ARRAY_SIZE(gt701_leds); ar7_led_data.leds = gt701_leds; } } /***************************************************************************** * Watchdog ****************************************************************************/ static struct resource ar7_wdt_res = { .name = "regs", .flags = IORESOURCE_MEM, .start = -1, /* Filled at runtime */ .end = -1, /* Filled at runtime */ }; static struct platform_device ar7_wdt = { .name = "ar7_wdt", .resource = &ar7_wdt_res, .num_resources = 1, }; /***************************************************************************** * Init ****************************************************************************/ static int __init ar7_register_uarts(void) { #ifdef CONFIG_SERIAL_8250 static struct uart_port uart_port __initdata; struct clk *bus_clk; int res; memset(&uart_port, 0, sizeof(struct uart_port)); bus_clk = clk_get(NULL, "bus"); if (IS_ERR(bus_clk)) panic("unable to get bus clk"); uart_port.type = PORT_AR7; uart_port.uartclk = clk_get_rate(bus_clk) / 2; uart_port.iotype = UPIO_MEM32; uart_port.flags = UPF_FIXED_TYPE | UPF_BOOT_AUTOCONF; uart_port.regshift = 2; uart_port.line = 0; uart_port.irq = AR7_IRQ_UART0; uart_port.mapbase = AR7_REGS_UART0; uart_port.membase = ioremap(uart_port.mapbase, 256); res = early_serial_setup(&uart_port); if (res) return res; /* Only TNETD73xx have a second serial port */ if (ar7_has_second_uart()) { uart_port.line = 1; uart_port.irq = AR7_IRQ_UART1; uart_port.mapbase = UR8_REGS_UART1; uart_port.membase = ioremap(uart_port.mapbase, 256); res = early_serial_setup(&uart_port); if (res) return res; } #endif return 0; } static void __init titan_fixup_devices(void) { /* Set vlynq0 data */ vlynq_low_data.reset_bit = 15; vlynq_low_data.gpio_bit = 14; /* Set vlynq1 data */ vlynq_high_data.reset_bit = 16; vlynq_high_data.gpio_bit = 7; /* Set vlynq0 resources */ vlynq_low_res[0].start = TITAN_REGS_VLYNQ0; vlynq_low_res[0].end = TITAN_REGS_VLYNQ0 + 0xff; vlynq_low_res[1].start = 33; vlynq_low_res[1].end = 33; vlynq_low_res[2].start = 0x0c000000; vlynq_low_res[2].end = 0x0fffffff; vlynq_low_res[3].start = 80; vlynq_low_res[3].end = 111; /* Set vlynq1 resources */ vlynq_high_res[0].start = TITAN_REGS_VLYNQ1; vlynq_high_res[0].end = TITAN_REGS_VLYNQ1 + 0xff; vlynq_high_res[1].start = 34; vlynq_high_res[1].end = 34; vlynq_high_res[2].start = 0x40000000; vlynq_high_res[2].end = 0x43ffffff; vlynq_high_res[3].start = 112; vlynq_high_res[3].end = 143; /* Set cpmac0 data */ cpmac_low_data.phy_mask = 0x40000000; /* Set cpmac1 data */ cpmac_high_data.phy_mask = 0x80000000; /* Set cpmac0 resources */ cpmac_low_res[0].start = TITAN_REGS_MAC0; cpmac_low_res[0].end = TITAN_REGS_MAC0 + 0x7ff; /* Set cpmac1 resources */ cpmac_high_res[0].start = TITAN_REGS_MAC1; cpmac_high_res[0].end = TITAN_REGS_MAC1 + 0x7ff; } static int __init ar7_register_devices(void) { void __iomem *bootcr; u32 val; int res; res = ar7_gpio_init(); if (res) pr_warn("unable to register gpios: %d\n", res); res = ar7_register_uarts(); if (res) pr_err("unable to setup uart(s): %d\n", res); res = platform_device_register(&physmap_flash); if (res) pr_warn("unable to register physmap-flash: %d\n", res); if (ar7_is_titan()) titan_fixup_devices(); ar7_device_disable(vlynq_low_data.reset_bit); res = platform_device_register(&vlynq_low); if (res) pr_warn("unable to register vlynq-low: %d\n", res); if (ar7_has_high_vlynq()) { ar7_device_disable(vlynq_high_data.reset_bit); res = platform_device_register(&vlynq_high); if (res) pr_warn("unable to register vlynq-high: %d\n", res); } if (ar7_has_high_cpmac()) { res = fixed_phy_add(PHY_POLL, cpmac_high.id, &fixed_phy_status); if (!res) { cpmac_get_mac(1, cpmac_high_data.dev_addr); res = platform_device_register(&cpmac_high); if (res) pr_warn("unable to register cpmac-high: %d\n", res); } else pr_warn("unable to add cpmac-high phy: %d\n", res); } else cpmac_low_data.phy_mask = 0xffffffff; res = fixed_phy_add(PHY_POLL, cpmac_low.id, &fixed_phy_status); if (!res) { cpmac_get_mac(0, cpmac_low_data.dev_addr); res = platform_device_register(&cpmac_low); if (res) pr_warn("unable to register cpmac-low: %d\n", res); } else pr_warn("unable to add cpmac-low phy: %d\n", res); detect_leds(); res = platform_device_register(&ar7_gpio_leds); if (res) pr_warn("unable to register leds: %d\n", res); res = platform_device_register(&ar7_udc); if (res) pr_warn("unable to register usb slave: %d\n", res); /* Register watchdog only if enabled in hardware */ bootcr = ioremap(AR7_REGS_DCL, 4); val = readl(bootcr); iounmap(bootcr); if (val & AR7_WDT_HW_ENA) { if (ar7_has_high_vlynq()) ar7_wdt_res.start = UR8_REGS_WDT; else ar7_wdt_res.start = AR7_REGS_WDT; ar7_wdt_res.end = ar7_wdt_res.start + 0x20; res = platform_device_register(&ar7_wdt); if (res) pr_warn("unable to register watchdog: %d\n", res); } return 0; } device_initcall(ar7_register_devices);
linux-master
arch/mips/ar7/platform.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2007 Felix Fietkau <[email protected]> * Copyright (C) 2007 Eugene Konev <[email protected]> * Copyright (C) 2009 Florian Fainelli <[email protected]> */ #include <linux/kernel.h> #include <linux/init.h> #include <linux/types.h> #include <linux/export.h> #include <linux/delay.h> #include <linux/gcd.h> #include <linux/io.h> #include <linux/err.h> #include <linux/clkdev.h> #include <linux/clk.h> #include <linux/clk-provider.h> #include <asm/addrspace.h> #include <asm/mach-ar7/ar7.h> #define BOOT_PLL_SOURCE_MASK 0x3 #define CPU_PLL_SOURCE_SHIFT 16 #define BUS_PLL_SOURCE_SHIFT 14 #define USB_PLL_SOURCE_SHIFT 18 #define DSP_PLL_SOURCE_SHIFT 22 #define BOOT_PLL_SOURCE_AFE 0 #define BOOT_PLL_SOURCE_BUS 0 #define BOOT_PLL_SOURCE_REF 1 #define BOOT_PLL_SOURCE_XTAL 2 #define BOOT_PLL_SOURCE_CPU 3 #define BOOT_PLL_BYPASS 0x00000020 #define BOOT_PLL_ASYNC_MODE 0x02000000 #define BOOT_PLL_2TO1_MODE 0x00008000 #define TNETD7200_CLOCK_ID_CPU 0 #define TNETD7200_CLOCK_ID_DSP 1 #define TNETD7200_CLOCK_ID_USB 2 #define TNETD7200_DEF_CPU_CLK 211000000 #define TNETD7200_DEF_DSP_CLK 125000000 #define TNETD7200_DEF_USB_CLK 48000000 struct tnetd7300_clock { u32 ctrl; #define PREDIV_MASK 0x001f0000 #define PREDIV_SHIFT 16 #define POSTDIV_MASK 0x0000001f u32 unused1[3]; u32 pll; #define MUL_MASK 0x0000f000 #define MUL_SHIFT 12 #define PLL_MODE_MASK 0x00000001 #define PLL_NDIV 0x00000800 #define PLL_DIV 0x00000002 #define PLL_STATUS 0x00000001 u32 unused2[3]; }; struct tnetd7300_clocks { struct tnetd7300_clock bus; struct tnetd7300_clock cpu; struct tnetd7300_clock usb; struct tnetd7300_clock dsp; }; struct tnetd7200_clock { u32 ctrl; u32 unused1[3]; #define DIVISOR_ENABLE_MASK 0x00008000 u32 mul; u32 prediv; u32 postdiv; u32 postdiv2; u32 unused2[6]; u32 cmd; u32 status; u32 cmden; u32 padding[15]; }; struct tnetd7200_clocks { struct tnetd7200_clock cpu; struct tnetd7200_clock dsp; struct tnetd7200_clock usb; }; struct clk_rate { u32 rate; }; static struct clk_rate bus_clk = { .rate = 125000000, }; static struct clk_rate cpu_clk = { .rate = 150000000, }; static void approximate(int base, int target, int *prediv, int *postdiv, int *mul) { int i, j, k, freq, res = target; for (i = 1; i <= 16; i++) for (j = 1; j <= 32; j++) for (k = 1; k <= 32; k++) { freq = abs(base / j * i / k - target); if (freq < res) { res = freq; *mul = i; *prediv = j; *postdiv = k; } } } static void calculate(int base, int target, int *prediv, int *postdiv, int *mul) { int tmp_gcd, tmp_base, tmp_freq; for (*prediv = 1; *prediv <= 32; (*prediv)++) { tmp_base = base / *prediv; tmp_gcd = gcd(target, tmp_base); *mul = target / tmp_gcd; *postdiv = tmp_base / tmp_gcd; if ((*mul < 1) || (*mul >= 16)) continue; if ((*postdiv > 0) & (*postdiv <= 32)) break; } if (base / *prediv * *mul / *postdiv != target) { approximate(base, target, prediv, postdiv, mul); tmp_freq = base / *prediv * *mul / *postdiv; printk(KERN_WARNING "Adjusted requested frequency %d to %d\n", target, tmp_freq); } printk(KERN_DEBUG "Clocks: prediv: %d, postdiv: %d, mul: %d\n", *prediv, *postdiv, *mul); } static int tnetd7300_dsp_clock(void) { u32 didr1, didr2; u8 rev = ar7_chip_rev(); didr1 = readl((void *)KSEG1ADDR(AR7_REGS_GPIO + 0x18)); didr2 = readl((void *)KSEG1ADDR(AR7_REGS_GPIO + 0x1c)); if (didr2 & (1 << 23)) return 0; if ((rev >= 0x23) && (rev != 0x57)) return 250000000; if ((((didr2 & 0x1fff) << 10) | ((didr1 & 0xffc00000) >> 22)) > 4208000) return 250000000; return 0; } static int tnetd7300_get_clock(u32 shift, struct tnetd7300_clock *clock, u32 *bootcr, u32 bus_clock) { int product; int base_clock = AR7_REF_CLOCK; u32 ctrl = readl(&clock->ctrl); u32 pll = readl(&clock->pll); int prediv = ((ctrl & PREDIV_MASK) >> PREDIV_SHIFT) + 1; int postdiv = (ctrl & POSTDIV_MASK) + 1; int divisor = prediv * postdiv; int mul = ((pll & MUL_MASK) >> MUL_SHIFT) + 1; switch ((*bootcr & (BOOT_PLL_SOURCE_MASK << shift)) >> shift) { case BOOT_PLL_SOURCE_BUS: base_clock = bus_clock; break; case BOOT_PLL_SOURCE_REF: base_clock = AR7_REF_CLOCK; break; case BOOT_PLL_SOURCE_XTAL: base_clock = AR7_XTAL_CLOCK; break; case BOOT_PLL_SOURCE_CPU: base_clock = cpu_clk.rate; break; } if (*bootcr & BOOT_PLL_BYPASS) return base_clock / divisor; if ((pll & PLL_MODE_MASK) == 0) return (base_clock >> (mul / 16 + 1)) / divisor; if ((pll & (PLL_NDIV | PLL_DIV)) == (PLL_NDIV | PLL_DIV)) { product = (mul & 1) ? (base_clock * mul) >> 1 : (base_clock * (mul - 1)) >> 2; return product / divisor; } if (mul == 16) return base_clock / divisor; return base_clock * mul / divisor; } static void tnetd7300_set_clock(u32 shift, struct tnetd7300_clock *clock, u32 *bootcr, u32 frequency) { int prediv, postdiv, mul; int base_clock = bus_clk.rate; switch ((*bootcr & (BOOT_PLL_SOURCE_MASK << shift)) >> shift) { case BOOT_PLL_SOURCE_BUS: base_clock = bus_clk.rate; break; case BOOT_PLL_SOURCE_REF: base_clock = AR7_REF_CLOCK; break; case BOOT_PLL_SOURCE_XTAL: base_clock = AR7_XTAL_CLOCK; break; case BOOT_PLL_SOURCE_CPU: base_clock = cpu_clk.rate; break; } calculate(base_clock, frequency, &prediv, &postdiv, &mul); writel(((prediv - 1) << PREDIV_SHIFT) | (postdiv - 1), &clock->ctrl); mdelay(1); writel(4, &clock->pll); while (readl(&clock->pll) & PLL_STATUS) ; writel(((mul - 1) << MUL_SHIFT) | (0xff << 3) | 0x0e, &clock->pll); mdelay(75); } static void __init tnetd7300_init_clocks(void) { u32 *bootcr = (u32 *)ioremap(AR7_REGS_DCL, 4); struct tnetd7300_clocks *clocks = ioremap(UR8_REGS_CLOCKS, sizeof(struct tnetd7300_clocks)); u32 dsp_clk; struct clk *clk; bus_clk.rate = tnetd7300_get_clock(BUS_PLL_SOURCE_SHIFT, &clocks->bus, bootcr, AR7_AFE_CLOCK); if (*bootcr & BOOT_PLL_ASYNC_MODE) cpu_clk.rate = tnetd7300_get_clock(CPU_PLL_SOURCE_SHIFT, &clocks->cpu, bootcr, AR7_AFE_CLOCK); else cpu_clk.rate = bus_clk.rate; dsp_clk = tnetd7300_dsp_clock(); if (dsp_clk == 250000000) tnetd7300_set_clock(DSP_PLL_SOURCE_SHIFT, &clocks->dsp, bootcr, dsp_clk); iounmap(clocks); iounmap(bootcr); clk = clk_register_fixed_rate(NULL, "cpu", NULL, 0, cpu_clk.rate); clkdev_create(clk, "cpu", NULL); clk = clk_register_fixed_rate(NULL, "dsp", NULL, 0, dsp_clk); clkdev_create(clk, "dsp", NULL); } static void tnetd7200_set_clock(int base, struct tnetd7200_clock *clock, int prediv, int postdiv, int postdiv2, int mul, u32 frequency) { printk(KERN_INFO "Clocks: base = %d, frequency = %u, prediv = %d, " "postdiv = %d, postdiv2 = %d, mul = %d\n", base, frequency, prediv, postdiv, postdiv2, mul); writel(0, &clock->ctrl); writel(DIVISOR_ENABLE_MASK | ((prediv - 1) & 0x1F), &clock->prediv); writel((mul - 1) & 0xF, &clock->mul); while (readl(&clock->status) & 0x1) ; /* nop */ writel(DIVISOR_ENABLE_MASK | ((postdiv - 1) & 0x1F), &clock->postdiv); writel(readl(&clock->cmden) | 1, &clock->cmden); writel(readl(&clock->cmd) | 1, &clock->cmd); while (readl(&clock->status) & 0x1) ; /* nop */ writel(DIVISOR_ENABLE_MASK | ((postdiv2 - 1) & 0x1F), &clock->postdiv2); writel(readl(&clock->cmden) | 1, &clock->cmden); writel(readl(&clock->cmd) | 1, &clock->cmd); while (readl(&clock->status) & 0x1) ; /* nop */ writel(readl(&clock->ctrl) | 1, &clock->ctrl); } static int tnetd7200_get_clock_base(int clock_id, u32 *bootcr) { if (*bootcr & BOOT_PLL_ASYNC_MODE) /* Async */ switch (clock_id) { case TNETD7200_CLOCK_ID_DSP: return AR7_REF_CLOCK; default: return AR7_AFE_CLOCK; } else /* Sync */ if (*bootcr & BOOT_PLL_2TO1_MODE) /* 2:1 */ switch (clock_id) { case TNETD7200_CLOCK_ID_DSP: return AR7_REF_CLOCK; default: return AR7_AFE_CLOCK; } else /* 1:1 */ return AR7_REF_CLOCK; } static void __init tnetd7200_init_clocks(void) { u32 *bootcr = (u32 *)ioremap(AR7_REGS_DCL, 4); struct tnetd7200_clocks *clocks = ioremap(AR7_REGS_CLOCKS, sizeof(struct tnetd7200_clocks)); int cpu_base, cpu_mul, cpu_prediv, cpu_postdiv; int dsp_base, dsp_mul, dsp_prediv, dsp_postdiv; int usb_base, usb_mul, usb_prediv, usb_postdiv; struct clk *clk; cpu_base = tnetd7200_get_clock_base(TNETD7200_CLOCK_ID_CPU, bootcr); dsp_base = tnetd7200_get_clock_base(TNETD7200_CLOCK_ID_DSP, bootcr); if (*bootcr & BOOT_PLL_ASYNC_MODE) { printk(KERN_INFO "Clocks: Async mode\n"); printk(KERN_INFO "Clocks: Setting DSP clock\n"); calculate(dsp_base, TNETD7200_DEF_DSP_CLK, &dsp_prediv, &dsp_postdiv, &dsp_mul); bus_clk.rate = ((dsp_base / dsp_prediv) * dsp_mul) / dsp_postdiv; tnetd7200_set_clock(dsp_base, &clocks->dsp, dsp_prediv, dsp_postdiv * 2, dsp_postdiv, dsp_mul * 2, bus_clk.rate); printk(KERN_INFO "Clocks: Setting CPU clock\n"); calculate(cpu_base, TNETD7200_DEF_CPU_CLK, &cpu_prediv, &cpu_postdiv, &cpu_mul); cpu_clk.rate = ((cpu_base / cpu_prediv) * cpu_mul) / cpu_postdiv; tnetd7200_set_clock(cpu_base, &clocks->cpu, cpu_prediv, cpu_postdiv, -1, cpu_mul, cpu_clk.rate); } else if (*bootcr & BOOT_PLL_2TO1_MODE) { printk(KERN_INFO "Clocks: Sync 2:1 mode\n"); printk(KERN_INFO "Clocks: Setting CPU clock\n"); calculate(cpu_base, TNETD7200_DEF_CPU_CLK, &cpu_prediv, &cpu_postdiv, &cpu_mul); cpu_clk.rate = ((cpu_base / cpu_prediv) * cpu_mul) / cpu_postdiv; tnetd7200_set_clock(cpu_base, &clocks->cpu, cpu_prediv, cpu_postdiv, -1, cpu_mul, cpu_clk.rate); printk(KERN_INFO "Clocks: Setting DSP clock\n"); calculate(dsp_base, TNETD7200_DEF_DSP_CLK, &dsp_prediv, &dsp_postdiv, &dsp_mul); bus_clk.rate = cpu_clk.rate / 2; tnetd7200_set_clock(dsp_base, &clocks->dsp, dsp_prediv, dsp_postdiv * 2, dsp_postdiv, dsp_mul * 2, bus_clk.rate); } else { printk(KERN_INFO "Clocks: Sync 1:1 mode\n"); printk(KERN_INFO "Clocks: Setting DSP clock\n"); calculate(dsp_base, TNETD7200_DEF_DSP_CLK, &dsp_prediv, &dsp_postdiv, &dsp_mul); bus_clk.rate = ((dsp_base / dsp_prediv) * dsp_mul) / dsp_postdiv; tnetd7200_set_clock(dsp_base, &clocks->dsp, dsp_prediv, dsp_postdiv * 2, dsp_postdiv, dsp_mul * 2, bus_clk.rate); cpu_clk.rate = bus_clk.rate; } printk(KERN_INFO "Clocks: Setting USB clock\n"); usb_base = bus_clk.rate; calculate(usb_base, TNETD7200_DEF_USB_CLK, &usb_prediv, &usb_postdiv, &usb_mul); tnetd7200_set_clock(usb_base, &clocks->usb, usb_prediv, usb_postdiv, -1, usb_mul, TNETD7200_DEF_USB_CLK); iounmap(clocks); iounmap(bootcr); clk = clk_register_fixed_rate(NULL, "cpu", NULL, 0, cpu_clk.rate); clkdev_create(clk, "cpu", NULL); clkdev_create(clk, "dsp", NULL); } void __init ar7_init_clocks(void) { struct clk *clk; switch (ar7_chip_id()) { case AR7_CHIP_7100: case AR7_CHIP_7200: tnetd7200_init_clocks(); break; case AR7_CHIP_7300: tnetd7300_init_clocks(); break; default: break; } clk = clk_register_fixed_rate(NULL, "bus", NULL, 0, bus_clk.rate); clkdev_create(clk, "bus", NULL); /* adjust vbus clock rate */ clk = clk_register_fixed_factor(NULL, "vbus", "bus", 0, 1, 2); clkdev_create(clk, "vbus", NULL); clkdev_create(clk, "cpmac", "cpmac.1"); clkdev_create(clk, "cpmac", "cpmac.1"); }
linux-master
arch/mips/ar7/clock.c
// SPDX-License-Identifier: GPL-2.0-only /* * Carsten Langgaard, [email protected] * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved. */ #include <linux/init.h> #include <linux/ioport.h> #include <linux/pm.h> #include <linux/time.h> #include <asm/reboot.h> #include <asm/mach-ar7/ar7.h> #include <asm/mach-ar7/prom.h> static void ar7_machine_restart(char *command) { u32 *softres_reg = ioremap(AR7_REGS_RESET + AR7_RESET_SOFTWARE, 1); writel(1, softres_reg); } static void ar7_machine_halt(void) { while (1) ; } static void ar7_machine_power_off(void) { u32 *power_reg = (u32 *)ioremap(AR7_REGS_POWER, 1); u32 power_state = readl(power_reg) | (3 << 30); writel(power_state, power_reg); ar7_machine_halt(); } const char *get_system_type(void) { u16 chip_id = ar7_chip_id(); u16 titan_variant_id = titan_chip_id(); switch (chip_id) { case AR7_CHIP_7100: return "TI AR7 (TNETD7100)"; case AR7_CHIP_7200: return "TI AR7 (TNETD7200)"; case AR7_CHIP_7300: return "TI AR7 (TNETD7300)"; case AR7_CHIP_TITAN: switch (titan_variant_id) { case TITAN_CHIP_1050: return "TI AR7 (TNETV1050)"; case TITAN_CHIP_1055: return "TI AR7 (TNETV1055)"; case TITAN_CHIP_1056: return "TI AR7 (TNETV1056)"; case TITAN_CHIP_1060: return "TI AR7 (TNETV1060)"; } fallthrough; default: return "TI AR7 (unknown)"; } } static int __init ar7_init_console(void) { return 0; } console_initcall(ar7_init_console); /* * Initializes basic routines and structures pointers, memory size (as * given by the bios and saves the command line. */ void __init plat_mem_setup(void) { unsigned long io_base; _machine_restart = ar7_machine_restart; _machine_halt = ar7_machine_halt; pm_power_off = ar7_machine_power_off; io_base = (unsigned long)ioremap(AR7_REGS_BASE, 0x10000); if (!io_base) panic("Can't remap IO base!"); set_io_port_base(io_base); prom_meminit(); printk(KERN_INFO "%s, ID: 0x%04x, Revision: 0x%02x\n", get_system_type(), ar7_chip_id(), ar7_chip_rev()); }
linux-master
arch/mips/ar7/setup.c
// SPDX-License-Identifier: GPL-2.0-only /* * Carsten Langgaard, [email protected] * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved. * * Setting up the clock on the MIPS boards. */ #include <linux/init.h> #include <linux/time.h> #include <linux/err.h> #include <linux/clk.h> #include <asm/time.h> #include <asm/mach-ar7/ar7.h> void __init plat_time_init(void) { struct clk *cpu_clk; /* Initialize ar7 clocks so the CPU clock frequency is correct */ ar7_init_clocks(); cpu_clk = clk_get(NULL, "cpu"); if (IS_ERR(cpu_clk)) { printk(KERN_ERR "unable to get cpu clock\n"); return; } mips_hpt_frequency = clk_get_rate(cpu_clk) / 2; }
linux-master
arch/mips/ar7/time.c
// SPDX-License-Identifier: GPL-2.0-only /* * Carsten Langgaard, [email protected] * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved. * * Putting things on the screen/serial line using YAMONs facilities. */ #include <linux/init.h> #include <linux/kernel.h> #include <linux/serial_reg.h> #include <linux/spinlock.h> #include <linux/export.h> #include <linux/string.h> #include <linux/io.h> #include <asm/bootinfo.h> #include <asm/setup.h> #include <asm/mach-ar7/ar7.h> #include <asm/mach-ar7/prom.h> #define MAX_ENTRY 80 struct env_var { char *name; char *value; }; static struct env_var adam2_env[MAX_ENTRY]; char *prom_getenv(const char *name) { int i; for (i = 0; (i < MAX_ENTRY) && adam2_env[i].name; i++) if (!strcmp(name, adam2_env[i].name)) return adam2_env[i].value; return NULL; } EXPORT_SYMBOL(prom_getenv); static void __init ar7_init_cmdline(int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) { strlcat(arcs_cmdline, argv[i], COMMAND_LINE_SIZE); if (i < (argc - 1)) strlcat(arcs_cmdline, " ", COMMAND_LINE_SIZE); } } struct psbl_rec { u32 psbl_size; u32 env_base; u32 env_size; u32 ffs_base; u32 ffs_size; }; static const char psp_env_version[] __initconst = "TIENV0.8"; struct psp_env_chunk { u8 num; u8 ctrl; u16 csum; u8 len; char data[11]; } __packed; struct psp_var_map_entry { u8 num; char *value; }; static const struct psp_var_map_entry psp_var_map[] = { { 1, "cpufrequency" }, { 2, "memsize" }, { 3, "flashsize" }, { 4, "modetty0" }, { 5, "modetty1" }, { 8, "maca" }, { 9, "macb" }, { 28, "sysfrequency" }, { 38, "mipsfrequency" }, }; /* Well-known variable (num is looked up in table above for matching variable name) Example: cpufrequency=211968000 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+--- | 01 |CTRL|CHECKSUM | 01 | _2 | _1 | _1 | _9 | _6 | _8 | _0 | _0 | _0 | \0 | FF +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+--- Name=Value pair in a single chunk Example: NAME=VALUE +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+--- | 00 |CTRL|CHECKSUM | 01 | _N | _A | _M | _E | _0 | _V | _A | _L | _U | _E | \0 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+--- Name=Value pair in 2 chunks (len is the number of chunks) Example: bootloaderVersion=1.3.7.15 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+--- | 00 |CTRL|CHECKSUM | 02 | _b | _o | _o | _t | _l | _o | _a | _d | _e | _r | _V +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+--- | _e | _r | _s | _i | _o | _n | \0 | _1 | _. | _3 | _. | _7 | _. | _1 | _5 | \0 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+--- Data is padded with 0xFF */ #define PSP_ENV_SIZE 4096 static char psp_env_data[PSP_ENV_SIZE] = { 0, }; static char * __init lookup_psp_var_map(u8 num) { int i; for (i = 0; i < ARRAY_SIZE(psp_var_map); i++) if (psp_var_map[i].num == num) return psp_var_map[i].value; return NULL; } static void __init add_adam2_var(char *name, char *value) { int i; for (i = 0; i < MAX_ENTRY; i++) { if (!adam2_env[i].name) { adam2_env[i].name = name; adam2_env[i].value = value; return; } else if (!strcmp(adam2_env[i].name, name)) { adam2_env[i].value = value; return; } } } static int __init parse_psp_env(void *psp_env_base) { int i, n; char *name, *value; struct psp_env_chunk *chunks = (struct psp_env_chunk *)psp_env_data; memcpy_fromio(chunks, psp_env_base, PSP_ENV_SIZE); i = 1; n = PSP_ENV_SIZE / sizeof(struct psp_env_chunk); while (i < n) { if ((chunks[i].num == 0xff) || ((i + chunks[i].len) > n)) break; value = chunks[i].data; if (chunks[i].num) { name = lookup_psp_var_map(chunks[i].num); } else { name = value; value += strlen(name) + 1; } if (name) add_adam2_var(name, value); i += chunks[i].len; } return 0; } static void __init ar7_init_env(struct env_var *env) { int i; struct psbl_rec *psbl = (struct psbl_rec *)(KSEG1ADDR(0x14000300)); void *psp_env = (void *)KSEG1ADDR(psbl->env_base); if (strcmp(psp_env, psp_env_version) == 0) { parse_psp_env(psp_env); } else { for (i = 0; i < MAX_ENTRY; i++, env++) if (env->name) add_adam2_var(env->name, env->value); } } static void __init console_config(void) { #ifdef CONFIG_SERIAL_8250_CONSOLE char console_string[40]; int baud = 0; char parity = '\0', bits = '\0', flow = '\0'; char *s, *p; if (strstr(arcs_cmdline, "console=")) return; s = prom_getenv("modetty0"); if (s) { baud = simple_strtoul(s, &p, 10); s = p; if (*s == ',') s++; if (*s) parity = *s++; if (*s == ',') s++; if (*s) bits = *s++; if (*s == ',') s++; if (*s == 'h') flow = 'r'; } if (baud == 0) baud = 38400; if (parity != 'n' && parity != 'o' && parity != 'e') parity = 'n'; if (bits != '7' && bits != '8') bits = '8'; if (flow == 'r') sprintf(console_string, " console=ttyS0,%d%c%c%c", baud, parity, bits, flow); else sprintf(console_string, " console=ttyS0,%d%c%c", baud, parity, bits); strlcat(arcs_cmdline, console_string, COMMAND_LINE_SIZE); #endif } void __init prom_init(void) { ar7_init_cmdline(fw_arg0, (char **)fw_arg1); ar7_init_env((struct env_var *)fw_arg2); console_config(); } #define PORT(offset) (KSEG1ADDR(AR7_REGS_UART0 + (offset * 4))) static inline unsigned int serial_in(int offset) { return readl((void *)PORT(offset)); } static inline void serial_out(int offset, int value) { writel(value, (void *)PORT(offset)); } void prom_putchar(char c) { while ((serial_in(UART_LSR) & UART_LSR_TEMT) == 0) ; serial_out(UART_TX, c); }
linux-master
arch/mips/ar7/prom.c
/* * Big Endian PROM code for SNI RM machines * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 2005-2006 Florian Lohoff ([email protected]) * Copyright (C) 2005-2006 Thomas Bogendoerfer ([email protected]) */ #include <linux/kernel.h> #include <linux/init.h> #include <linux/memblock.h> #include <linux/string.h> #include <linux/console.h> #include <asm/addrspace.h> #include <asm/sni.h> #include <asm/mipsprom.h> #include <asm/mipsregs.h> #include <asm/bootinfo.h> #include <asm/setup.h> /* special SNI prom calls */ /* * This does not exist in all proms - SINIX compares * the prom env variable "version" against "2.0008" * or greater. If lesser it tries to probe interesting * registers */ #define PROM_GET_MEMCONF 58 #define PROM_GET_HWCONF 61 #define PROM_VEC (u64 *)CKSEG1ADDR(0x1fc00000) #define PROM_ENTRY(x) (PROM_VEC + (x)) #define ___prom_putchar ((int *(*)(int))PROM_ENTRY(PROM_PUTCHAR)) #define ___prom_getenv ((char *(*)(char *))PROM_ENTRY(PROM_GETENV)) #define ___prom_get_memconf ((void (*)(void *))PROM_ENTRY(PROM_GET_MEMCONF)) #define ___prom_get_hwconf ((u32 (*)(void))PROM_ENTRY(PROM_GET_HWCONF)) #ifdef CONFIG_64BIT /* O32 stack has to be 8-byte aligned. */ static u64 o32_stk[4096]; #define O32_STK (&o32_stk[ARRAY_SIZE(o32_stk)]) #define __PROM_O32(fun, arg) fun arg __asm__(#fun); \ __asm__(#fun " = call_o32") int __PROM_O32(__prom_putchar, (int *(*)(int), void *, int)); char *__PROM_O32(__prom_getenv, (char *(*)(char *), void *, char *)); void __PROM_O32(__prom_get_memconf, (void (*)(void *), void *, void *)); u32 __PROM_O32(__prom_get_hwconf, (u32 (*)(void), void *)); #define _prom_putchar(x) __prom_putchar(___prom_putchar, O32_STK, x) #define _prom_getenv(x) __prom_getenv(___prom_getenv, O32_STK, x) #define _prom_get_memconf(x) __prom_get_memconf(___prom_get_memconf, O32_STK, x) #define _prom_get_hwconf() __prom_get_hwconf(___prom_get_hwconf, O32_STK) #else #define _prom_putchar(x) ___prom_putchar(x) #define _prom_getenv(x) ___prom_getenv(x) #define _prom_get_memconf(x) ___prom_get_memconf(x) #define _prom_get_hwconf(x) ___prom_get_hwconf(x) #endif void prom_putchar(char c) { _prom_putchar(c); } char *prom_getenv(char *s) { return _prom_getenv(s); } void *prom_get_hwconf(void) { u32 hwconf = _prom_get_hwconf(); if (hwconf == 0xffffffff) return NULL; return (void *)CKSEG1ADDR(hwconf); } /* * /proc/cpuinfo system type * */ char *system_type = "Unknown"; const char *get_system_type(void) { return system_type; } static void __init sni_mem_init(void) { int i, memsize; struct membank { u32 size; u32 base; u32 size2; u32 pad1; u32 pad2; } memconf[8]; int brd_type = *(unsigned char *)SNI_IDPROM_BRDTYPE; /* MemSIZE from prom in 16MByte chunks */ memsize = *((unsigned char *) SNI_IDPROM_MEMSIZE) * 16; pr_debug("IDProm memsize: %u MByte\n", memsize); /* get memory bank layout from prom */ _prom_get_memconf(&memconf); pr_debug("prom_get_mem_conf memory configuration:\n"); for (i = 0; i < 8 && memconf[i].size; i++) { if (brd_type == SNI_BRD_PCI_TOWER || brd_type == SNI_BRD_PCI_TOWER_CPLUS) { if (memconf[i].base >= 0x20000000 && memconf[i].base < 0x30000000) memconf[i].base -= 0x20000000; } pr_debug("Bank%d: %08x @ %08x\n", i, memconf[i].size, memconf[i].base); memblock_add(memconf[i].base, memconf[i].size); } } void __init prom_init(void) { int argc = fw_arg0; u32 *argv = (u32 *)CKSEG0ADDR(fw_arg1); int i; sni_mem_init(); /* copy prom cmdline parameters to kernel cmdline */ for (i = 1; i < argc; i++) { strcat(arcs_cmdline, (char *)CKSEG0ADDR(argv[i])); if (i < (argc - 1)) strcat(arcs_cmdline, " "); } }
linux-master
arch/mips/fw/sni/sniprom.c
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. */ #include <linux/init.h> #include <linux/kernel.h> #include <linux/string.h> #include <asm/addrspace.h> #include <asm/fw/fw.h> int fw_argc; int *_fw_argv; int *_fw_envp; #ifndef CONFIG_HAVE_PLAT_FW_INIT_CMDLINE void __init fw_init_cmdline(void) { int i; /* Validate command line parameters. */ if ((fw_arg0 >= CKSEG0) || (fw_arg1 < CKSEG0)) { fw_argc = 0; _fw_argv = NULL; } else { fw_argc = (fw_arg0 & 0x0000ffff); _fw_argv = (int *)fw_arg1; } /* Validate environment pointer. */ if (fw_arg2 < CKSEG0) _fw_envp = NULL; else _fw_envp = (int *)fw_arg2; for (i = 1; i < fw_argc; i++) { strlcat(arcs_cmdline, fw_argv(i), COMMAND_LINE_SIZE); if (i < (fw_argc - 1)) strlcat(arcs_cmdline, " ", COMMAND_LINE_SIZE); } } #endif char * __init fw_getcmdline(void) { return &(arcs_cmdline[0]); } char *fw_getenv(char *envname) { char *result = NULL; if (_fw_envp != NULL && fw_envp(0) != NULL) { /* * Return a pointer to the given environment variable. * YAMON uses "name", "value" pairs, while U-Boot uses * "name=value". */ int i, yamon, index = 0; yamon = (strchr(fw_envp(index), '=') == NULL); i = strlen(envname); while (fw_envp(index)) { if (strncmp(envname, fw_envp(index), i) == 0) { if (yamon) { result = fw_envp(index + 1); break; } else if (fw_envp(index)[i] == '=') { result = fw_envp(index) + i + 1; break; } } /* Increment array index. */ if (yamon) index += 2; else index += 1; } } return result; } unsigned long fw_getenvl(char *envname) { unsigned long envl = 0UL; char *str; int tmp; str = fw_getenv(envname); if (str) { tmp = kstrtoul(str, 0, &envl); if (tmp) envl = 0; } return envl; }
linux-master
arch/mips/fw/lib/cmdline.c
// SPDX-License-Identifier: GPL-2.0 /* * memory.c: PROM library functions for acquiring/using memory descriptors * given to us from the ARCS firmware. * * Copyright (C) 1996 by David S. Miller * Copyright (C) 1999, 2000, 2001 by Ralf Baechle * Copyright (C) 1999, 2000 by Silicon Graphics, Inc. * * PROM library functions for acquiring/using memory descriptors given to us * from the ARCS firmware. This is only used when CONFIG_ARC_MEMORY is set * because on some machines like SGI IP27 the ARC memory configuration data * completely bogus and alternate easier to use mechanisms are available. */ #include <linux/init.h> #include <linux/kernel.h> #include <linux/types.h> #include <linux/sched.h> #include <linux/mm.h> #include <linux/memblock.h> #include <linux/swap.h> #include <asm/sgialib.h> #include <asm/page.h> #include <asm/bootinfo.h> #undef DEBUG #define MAX_PROM_MEM 5 static phys_addr_t prom_mem_base[MAX_PROM_MEM] __initdata; static phys_addr_t prom_mem_size[MAX_PROM_MEM] __initdata; static unsigned int nr_prom_mem __initdata; /* * For ARC firmware memory functions the unit of measuring memory is always * a 4k page of memory */ #define ARC_PAGE_SHIFT 12 struct linux_mdesc * __init ArcGetMemoryDescriptor(struct linux_mdesc *Current) { return (struct linux_mdesc *) ARC_CALL1(get_mdesc, Current); } #ifdef DEBUG /* convenient for debugging */ static char *arcs_mtypes[8] = { "Exception Block", "ARCS Romvec Page", "Free/Contig RAM", "Generic Free RAM", "Bad Memory", "Standalone Program Pages", "ARCS Temp Storage Area", "ARCS Permanent Storage Area" }; static char *arc_mtypes[8] = { "Exception Block", "SystemParameterBlock", "FreeMemory", "Bad Memory", "LoadedProgram", "FirmwareTemporary", "FirmwarePermanent", "FreeContiguous" }; #define mtypes(a) (prom_flags & PROM_FLAG_ARCS) ? arcs_mtypes[a.arcs] \ : arc_mtypes[a.arc] #endif enum { mem_free, mem_prom_used, mem_reserved }; static inline int memtype_classify_arcs(union linux_memtypes type) { switch (type.arcs) { case arcs_fcontig: case arcs_free: return mem_free; case arcs_atmp: return mem_prom_used; case arcs_eblock: case arcs_rvpage: case arcs_bmem: case arcs_prog: case arcs_aperm: return mem_reserved; default: BUG(); } while(1); /* Nuke warning. */ } static inline int memtype_classify_arc(union linux_memtypes type) { switch (type.arc) { case arc_free: case arc_fcontig: return mem_free; case arc_atmp: return mem_prom_used; case arc_eblock: case arc_rvpage: case arc_bmem: case arc_prog: case arc_aperm: return mem_reserved; default: BUG(); } while(1); /* Nuke warning. */ } static int __init prom_memtype_classify(union linux_memtypes type) { if (prom_flags & PROM_FLAG_ARCS) /* SGI is ``different'' ... */ return memtype_classify_arcs(type); return memtype_classify_arc(type); } void __weak __init prom_meminit(void) { struct linux_mdesc *p; #ifdef DEBUG int i = 0; printk("ARCS MEMORY DESCRIPTOR dump:\n"); p = ArcGetMemoryDescriptor(PROM_NULL_MDESC); while(p) { printk("[%d,%p]: base<%08lx> pages<%08lx> type<%s>\n", i, p, p->base, p->pages, mtypes(p->type)); p = ArcGetMemoryDescriptor(p); i++; } #endif nr_prom_mem = 0; p = PROM_NULL_MDESC; while ((p = ArcGetMemoryDescriptor(p))) { unsigned long base, size; long type; base = p->base << ARC_PAGE_SHIFT; size = p->pages << ARC_PAGE_SHIFT; type = prom_memtype_classify(p->type); /* ignore mirrored RAM on IP28/IP30 */ if (base < PHYS_OFFSET) continue; memblock_add(base, size); if (type == mem_reserved) memblock_reserve(base, size); if (type == mem_prom_used) { memblock_reserve(base, size); if (nr_prom_mem >= 5) { pr_err("Too many ROM DATA regions"); continue; } prom_mem_base[nr_prom_mem] = base; prom_mem_size[nr_prom_mem] = size; nr_prom_mem++; } } } void __weak __init prom_cleanup(void) { } void __init prom_free_prom_memory(void) { int i; if (prom_flags & PROM_FLAG_DONT_FREE_TEMP) return; for (i = 0; i < nr_prom_mem; i++) { free_init_pages("prom memory", prom_mem_base[i], prom_mem_base[i] + prom_mem_size[i]); } /* * at this point it isn't safe to call PROM functions * give platforms a way to do PROM cleanups */ prom_cleanup(); }
linux-master
arch/mips/fw/arc/memory.c
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * identify.c: identify machine by looking up system identifier * * Copyright (C) 1998 Thomas Bogendoerfer * * This code is based on arch/mips/sgi/kernel/system.c, which is * * Copyright (C) 1996 David S. Miller ([email protected]) */ #include <linux/bug.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/types.h> #include <linux/string.h> #include <asm/sgialib.h> #include <asm/bootinfo.h> struct smatch { char *arcname; char *liname; int flags; }; static struct smatch mach_table[] = { { .arcname = "SGI-IP22", .liname = "SGI Indy", .flags = PROM_FLAG_ARCS, }, { .arcname = "SGI-IP28", .liname = "SGI IP28", .flags = PROM_FLAG_ARCS, }, { .arcname = "SGI-IP30", .liname = "SGI Octane", .flags = PROM_FLAG_ARCS, }, { .arcname = "SGI-IP32", .liname = "SGI O2", .flags = PROM_FLAG_ARCS, }, { .arcname = "Microsoft-Jazz", .liname = "Jazz MIPS_Magnum_4000", .flags = 0, }, { .arcname = "PICA-61", .liname = "Jazz Acer_PICA_61", .flags = 0, }, { .arcname = "RM200PCI", .liname = "SNI RM200_PCI", .flags = PROM_FLAG_DONT_FREE_TEMP, }, { .arcname = "RM200PCI-R5K", .liname = "SNI RM200_PCI-R5K", .flags = PROM_FLAG_DONT_FREE_TEMP, } }; int prom_flags; static struct smatch * __init string_to_mach(const char *s) { int i; for (i = 0; i < ARRAY_SIZE(mach_table); i++) { if (!strcmp(s, mach_table[i].arcname)) return &mach_table[i]; } panic("Yeee, could not determine architecture type <%s>", s); } char *system_type; const char *get_system_type(void) { return system_type; } static pcomponent * __init ArcGetChild(pcomponent *Current) { return (pcomponent *) ARC_CALL1(child_component, Current); } void __init prom_identify_arch(void) { pcomponent *p; struct smatch *mach; const char *iname; /* * The root component tells us what machine architecture we have here. */ p = ArcGetChild(PROM_NULL_COMPONENT); if (p == NULL) { iname = "Unknown"; } else iname = (char *) (long) p->iname; printk("ARCH: %s\n", iname); mach = string_to_mach(iname); system_type = mach->liname; prom_flags = mach->flags; }
linux-master
arch/mips/fw/arc/identify.c
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * PROM library initialisation code. * * Copyright (C) 1996 David S. Miller ([email protected]) */ #include <linux/init.h> #include <linux/kernel.h> #include <asm/bootinfo.h> #include <asm/sgialib.h> #include <asm/smp-ops.h> #undef DEBUG_PROM_INIT /* Master romvec interface. */ struct linux_romvec *romvec; #if defined(CONFIG_64BIT) && defined(CONFIG_FW_ARC32) /* stack for calling 32bit ARC prom */ u64 o32_stk[4096]; #endif void __init prom_init(void) { PSYSTEM_PARAMETER_BLOCK pb = PROMBLOCK; romvec = ROMVECTOR; if (pb->magic != 0x53435241) { printk(KERN_CRIT "Aieee, bad prom vector magic %08lx\n", (unsigned long) pb->magic); while(1) ; } prom_init_cmdline(fw_arg0, (LONG *)fw_arg1); prom_identify_arch(); printk(KERN_INFO "PROMLIB: ARC firmware Version %d Revision %d\n", pb->ver, pb->rev); prom_meminit(); #ifdef DEBUG_PROM_INIT pr_info("Press a key to reboot\n"); ArcRead(0, &c, 1, &cnt); ArcEnterInteractiveMode(); #endif }
linux-master
arch/mips/fw/arc/init.c
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 1996 David S. Miller ([email protected]) * Compatibility with board caches, Ulf Carlsson */ #include <linux/kernel.h> #include <asm/sgialib.h> #include <asm/bcache.h> #include <asm/setup.h> #if defined(CONFIG_64BIT) && defined(CONFIG_FW_ARC32) /* * For 64bit kernels working with a 32bit ARC PROM pointer arguments * for ARC calls need to reside in CKEG0/1. But as soon as the kernel * switches to it's first kernel thread stack is set to an address in * XKPHYS, so anything on stack can't be used anymore. This is solved * by using a * static declartion variables are put into BSS, which is * linked to a CKSEG0 address. Since this is only used on UP platforms * there is not spinlock needed */ #define O32_STATIC static #else #define O32_STATIC #endif /* * IP22 boardcache is not compatible with board caches. Thus we disable it * during romvec action. Since r4xx0.c is always compiled and linked with your * kernel, this shouldn't cause any harm regardless what MIPS processor you * have. * * The ARC write and read functions seem to interfere with the serial lines * in some way. You should be careful with them. */ void prom_putchar(char c) { O32_STATIC ULONG cnt; O32_STATIC CHAR it; it = c; bc_disable(); ArcWrite(1, &it, 1, &cnt); bc_enable(); } char prom_getchar(void) { O32_STATIC ULONG cnt; O32_STATIC CHAR c; bc_disable(); ArcRead(0, &c, 1, &cnt); bc_enable(); return c; }
linux-master
arch/mips/fw/arc/promlib.c
// SPDX-License-Identifier: GPL-2.0 /* * Wrap-around code for a console using the * ARC io-routines. * * Copyright (c) 1998 Harald Koerfgen * Copyright (c) 2001 Ralf Baechle * Copyright (c) 2002 Thiemo Seufer */ #include <linux/tty.h> #include <linux/major.h> #include <linux/init.h> #include <linux/console.h> #include <linux/fs.h> #include <asm/setup.h> #include <asm/sgialib.h> static void prom_console_write(struct console *co, const char *s, unsigned count) { /* Do each character */ while (count--) { if (*s == '\n') prom_putchar('\r'); prom_putchar(*s++); } } static int prom_console_setup(struct console *co, char *options) { if (prom_flags & PROM_FLAG_USE_AS_CONSOLE) return 0; return -ENODEV; } static struct console arc_cons = { .name = "arc", .write = prom_console_write, .setup = prom_console_setup, .flags = CON_PRINTBUFFER, .index = -1, }; /* * Register console. */ static int __init arc_console_init(void) { register_console(&arc_cons); return 0; } console_initcall(arc_console_init);
linux-master
arch/mips/fw/arc/arc_con.c
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * env.c: ARCS environment variable routines. * * Copyright (C) 1996 David S. Miller ([email protected]) */ #include <linux/init.h> #include <linux/kernel.h> #include <linux/string.h> #include <asm/fw/arc/types.h> #include <asm/sgialib.h> PCHAR __init ArcGetEnvironmentVariable(CHAR *name) { return (CHAR *) ARC_CALL1(get_evar, name); }
linux-master
arch/mips/fw/arc/env.c
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * cmdline.c: Kernel command line creation using ARCS argc/argv. * * Copyright (C) 1996 David S. Miller ([email protected]) */ #include <linux/bug.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/string.h> #include <asm/sgialib.h> #include <asm/bootinfo.h> #undef DEBUG_CMDLINE /* * A 32-bit ARC PROM pass arguments and environment as 32-bit pointer. * These macro take care of sign extension. */ #define prom_argv(index) ((char *) (long)argv[(index)]) static char *ignored[] = { "ConsoleIn=", "ConsoleOut=", "SystemPartition=", "OSLoader=", "OSLoadPartition=", "OSLoadFilename=", "OSLoadOptions=" }; static char *used_arc[][2] = { { "OSLoadPartition=", "root=" }, { "OSLoadOptions=", "" } }; static char __init *move_firmware_args(int argc, LONG *argv, char *cp) { char *s; int actr, i; actr = 1; /* Always ignore argv[0] */ while (actr < argc) { for(i = 0; i < ARRAY_SIZE(used_arc); i++) { int len = strlen(used_arc[i][0]); if (!strncmp(prom_argv(actr), used_arc[i][0], len)) { /* Ok, we want it. First append the replacement... */ strcat(cp, used_arc[i][1]); cp += strlen(used_arc[i][1]); /* ... and now the argument */ s = strchr(prom_argv(actr), '='); if (s) { s++; strcpy(cp, s); cp += strlen(s); } *cp++ = ' '; break; } } actr++; } return cp; } void __init prom_init_cmdline(int argc, LONG *argv) { char *cp; int actr, i; actr = 1; /* Always ignore argv[0] */ cp = arcs_cmdline; /* * Move ARC variables to the beginning to make sure they can be * overridden by later arguments. */ cp = move_firmware_args(argc, argv, cp); while (actr < argc) { for (i = 0; i < ARRAY_SIZE(ignored); i++) { int len = strlen(ignored[i]); if (!strncmp(prom_argv(actr), ignored[i], len)) goto pic_cont; } /* Ok, we want it. */ strcpy(cp, prom_argv(actr)); cp += strlen(prom_argv(actr)); *cp++ = ' '; pic_cont: actr++; } if (cp != arcs_cmdline) /* get rid of trailing space */ --cp; *cp = '\0'; #ifdef DEBUG_CMDLINE printk(KERN_DEBUG "prom cmdline: %s\n", arcs_cmdline); #endif }
linux-master
arch/mips/fw/arc/cmdline.c
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * ARC firmware interface. * * Copyright (C) 1994, 1995, 1996, 1999 Ralf Baechle * Copyright (C) 1999 Silicon Graphics, Inc. */ #include <asm/fw/arc/types.h> #include <asm/sgialib.h> LONG ArcRead(ULONG FileID, VOID *Buffer, ULONG N, ULONG *Count) { return ARC_CALL4(read, FileID, Buffer, N, Count); } LONG ArcWrite(ULONG FileID, PVOID Buffer, ULONG N, PULONG Count) { return ARC_CALL4(write, FileID, Buffer, N, Count); }
linux-master
arch/mips/fw/arc/file.c
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Miscellaneous ARCS PROM routines. * * Copyright (C) 1996 David S. Miller ([email protected]) * Copyright (C) 1999 Ralf Baechle ([email protected]) * Copyright (C) 1999 Silicon Graphics, Inc. */ #include <linux/compiler.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/irqflags.h> #include <asm/bcache.h> #include <asm/fw/arc/types.h> #include <asm/sgialib.h> #include <asm/bootinfo.h> VOID __noreturn ArcEnterInteractiveMode(VOID) { bc_disable(); local_irq_disable(); ARC_CALL0(imode); unreachable(); } DISPLAY_STATUS * __init ArcGetDisplayStatus(ULONG FileID) { return (DISPLAY_STATUS *) ARC_CALL1(GetDisplayStatus, FileID); }
linux-master
arch/mips/fw/arc/misc.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2000, 2001, 2002 Broadcom Corporation */ /* * * Broadcom Common Firmware Environment (CFE) * * This module contains device function stubs (small routines to * call the standard "iocb" interface entry point to CFE). * There should be one routine here per iocb function call. * * Authors: Mitch Lichtenberg, Chris Demetriou */ #include <linux/init.h> #include <linux/kernel.h> #include <linux/printk.h> #include <asm/mipsregs.h> #include <asm/fw/cfe/cfe_api.h> #include "cfe_api_int.h" unsigned long __initdata cfe_seal; /* Cast from a native pointer to a cfe_xptr_t and back. */ #define XPTR_FROM_NATIVE(n) ((cfe_xptr_t) (intptr_t) (n)) #define NATIVE_FROM_XPTR(x) ((void *) (intptr_t) (x)) int cfe_iocb_dispatch(struct cfe_xiocb *xiocb); /* * Declare the dispatch function with args of "intptr_t". * This makes sure whatever model we're compiling in * puts the pointers in a single register. For example, * combining -mlong64 and -mips1 or -mips2 would lead to * trouble, since the handle and IOCB pointer will be * passed in two registers each, and CFE expects one. */ static int (*cfe_dispfunc) (intptr_t handle, intptr_t xiocb); static u64 cfe_handle; int cfe_init(u64 handle, u64 ept) { cfe_dispfunc = NATIVE_FROM_XPTR(ept); cfe_handle = handle; return 0; } int cfe_iocb_dispatch(struct cfe_xiocb * xiocb) { if (!cfe_dispfunc) return -1; return (*cfe_dispfunc) ((intptr_t) cfe_handle, (intptr_t) xiocb); } int cfe_close(int handle) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_DEV_CLOSE; xiocb.xiocb_status = 0; xiocb.xiocb_handle = handle; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = 0; cfe_iocb_dispatch(&xiocb); return xiocb.xiocb_status; } int cfe_cpu_start(int cpu, void (*fn) (void), long sp, long gp, long a1) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_FW_CPUCTL; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_cpuctl); xiocb.plist.xiocb_cpuctl.cpu_number = cpu; xiocb.plist.xiocb_cpuctl.cpu_command = CFE_CPU_CMD_START; xiocb.plist.xiocb_cpuctl.gp_val = gp; xiocb.plist.xiocb_cpuctl.sp_val = sp; xiocb.plist.xiocb_cpuctl.a1_val = a1; xiocb.plist.xiocb_cpuctl.start_addr = (long) fn; cfe_iocb_dispatch(&xiocb); return xiocb.xiocb_status; } int cfe_cpu_stop(int cpu) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_FW_CPUCTL; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_cpuctl); xiocb.plist.xiocb_cpuctl.cpu_number = cpu; xiocb.plist.xiocb_cpuctl.cpu_command = CFE_CPU_CMD_STOP; cfe_iocb_dispatch(&xiocb); return xiocb.xiocb_status; } int cfe_enumenv(int idx, char *name, int namelen, char *val, int vallen) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_ENV_SET; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_envbuf); xiocb.plist.xiocb_envbuf.enum_idx = idx; xiocb.plist.xiocb_envbuf.name_ptr = XPTR_FROM_NATIVE(name); xiocb.plist.xiocb_envbuf.name_length = namelen; xiocb.plist.xiocb_envbuf.val_ptr = XPTR_FROM_NATIVE(val); xiocb.plist.xiocb_envbuf.val_length = vallen; cfe_iocb_dispatch(&xiocb); return xiocb.xiocb_status; } int cfe_enummem(int idx, int flags, u64 *start, u64 *length, u64 *type) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_FW_MEMENUM; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = flags; xiocb.xiocb_psize = sizeof(struct xiocb_meminfo); xiocb.plist.xiocb_meminfo.mi_idx = idx; cfe_iocb_dispatch(&xiocb); if (xiocb.xiocb_status < 0) return xiocb.xiocb_status; *start = xiocb.plist.xiocb_meminfo.mi_addr; *length = xiocb.plist.xiocb_meminfo.mi_size; *type = xiocb.plist.xiocb_meminfo.mi_type; return 0; } int cfe_exit(int warm, int status) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_FW_RESTART; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = warm ? CFE_FLG_WARMSTART : 0; xiocb.xiocb_psize = sizeof(struct xiocb_exitstat); xiocb.plist.xiocb_exitstat.status = status; cfe_iocb_dispatch(&xiocb); return xiocb.xiocb_status; } int cfe_flushcache(int flg) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_FW_FLUSHCACHE; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = flg; xiocb.xiocb_psize = 0; cfe_iocb_dispatch(&xiocb); return xiocb.xiocb_status; } int cfe_getdevinfo(char *name) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_DEV_GETINFO; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_buffer); xiocb.plist.xiocb_buffer.buf_offset = 0; xiocb.plist.xiocb_buffer.buf_ptr = XPTR_FROM_NATIVE(name); xiocb.plist.xiocb_buffer.buf_length = strlen(name); cfe_iocb_dispatch(&xiocb); if (xiocb.xiocb_status < 0) return xiocb.xiocb_status; return xiocb.plist.xiocb_buffer.buf_ioctlcmd; } int cfe_getenv(char *name, char *dest, int destlen) { struct cfe_xiocb xiocb; *dest = 0; xiocb.xiocb_fcode = CFE_CMD_ENV_GET; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_envbuf); xiocb.plist.xiocb_envbuf.enum_idx = 0; xiocb.plist.xiocb_envbuf.name_ptr = XPTR_FROM_NATIVE(name); xiocb.plist.xiocb_envbuf.name_length = strlen(name); xiocb.plist.xiocb_envbuf.val_ptr = XPTR_FROM_NATIVE(dest); xiocb.plist.xiocb_envbuf.val_length = destlen; cfe_iocb_dispatch(&xiocb); return xiocb.xiocb_status; } int cfe_getfwinfo(cfe_fwinfo_t * info) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_FW_GETINFO; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_fwinfo); cfe_iocb_dispatch(&xiocb); if (xiocb.xiocb_status < 0) return xiocb.xiocb_status; info->fwi_version = xiocb.plist.xiocb_fwinfo.fwi_version; info->fwi_totalmem = xiocb.plist.xiocb_fwinfo.fwi_totalmem; info->fwi_flags = xiocb.plist.xiocb_fwinfo.fwi_flags; info->fwi_boardid = xiocb.plist.xiocb_fwinfo.fwi_boardid; info->fwi_bootarea_va = xiocb.plist.xiocb_fwinfo.fwi_bootarea_va; info->fwi_bootarea_pa = xiocb.plist.xiocb_fwinfo.fwi_bootarea_pa; info->fwi_bootarea_size = xiocb.plist.xiocb_fwinfo.fwi_bootarea_size; return 0; } int cfe_getstdhandle(int flg) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_DEV_GETHANDLE; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = flg; xiocb.xiocb_psize = 0; cfe_iocb_dispatch(&xiocb); if (xiocb.xiocb_status < 0) return xiocb.xiocb_status; return xiocb.xiocb_handle; } int64_t cfe_getticks(void) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_FW_GETTIME; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_time); xiocb.plist.xiocb_time.ticks = 0; cfe_iocb_dispatch(&xiocb); return xiocb.plist.xiocb_time.ticks; } int cfe_inpstat(int handle) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_DEV_INPSTAT; xiocb.xiocb_status = 0; xiocb.xiocb_handle = handle; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_inpstat); xiocb.plist.xiocb_inpstat.inp_status = 0; cfe_iocb_dispatch(&xiocb); if (xiocb.xiocb_status < 0) return xiocb.xiocb_status; return xiocb.plist.xiocb_inpstat.inp_status; } int cfe_ioctl(int handle, unsigned int ioctlnum, unsigned char *buffer, int length, int *retlen, u64 offset) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_DEV_IOCTL; xiocb.xiocb_status = 0; xiocb.xiocb_handle = handle; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_buffer); xiocb.plist.xiocb_buffer.buf_offset = offset; xiocb.plist.xiocb_buffer.buf_ioctlcmd = ioctlnum; xiocb.plist.xiocb_buffer.buf_ptr = XPTR_FROM_NATIVE(buffer); xiocb.plist.xiocb_buffer.buf_length = length; cfe_iocb_dispatch(&xiocb); if (retlen) *retlen = xiocb.plist.xiocb_buffer.buf_retlen; return xiocb.xiocb_status; } int cfe_open(char *name) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_DEV_OPEN; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_buffer); xiocb.plist.xiocb_buffer.buf_offset = 0; xiocb.plist.xiocb_buffer.buf_ptr = XPTR_FROM_NATIVE(name); xiocb.plist.xiocb_buffer.buf_length = strlen(name); cfe_iocb_dispatch(&xiocb); if (xiocb.xiocb_status < 0) return xiocb.xiocb_status; return xiocb.xiocb_handle; } int cfe_read(int handle, unsigned char *buffer, int length) { return cfe_readblk(handle, 0, buffer, length); } int cfe_readblk(int handle, s64 offset, unsigned char *buffer, int length) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_DEV_READ; xiocb.xiocb_status = 0; xiocb.xiocb_handle = handle; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_buffer); xiocb.plist.xiocb_buffer.buf_offset = offset; xiocb.plist.xiocb_buffer.buf_ptr = XPTR_FROM_NATIVE(buffer); xiocb.plist.xiocb_buffer.buf_length = length; cfe_iocb_dispatch(&xiocb); if (xiocb.xiocb_status < 0) return xiocb.xiocb_status; return xiocb.plist.xiocb_buffer.buf_retlen; } int cfe_setenv(char *name, char *val) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_ENV_SET; xiocb.xiocb_status = 0; xiocb.xiocb_handle = 0; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_envbuf); xiocb.plist.xiocb_envbuf.enum_idx = 0; xiocb.plist.xiocb_envbuf.name_ptr = XPTR_FROM_NATIVE(name); xiocb.plist.xiocb_envbuf.name_length = strlen(name); xiocb.plist.xiocb_envbuf.val_ptr = XPTR_FROM_NATIVE(val); xiocb.plist.xiocb_envbuf.val_length = strlen(val); cfe_iocb_dispatch(&xiocb); return xiocb.xiocb_status; } int cfe_write(int handle, const char *buffer, int length) { return cfe_writeblk(handle, 0, buffer, length); } int cfe_writeblk(int handle, s64 offset, const char *buffer, int length) { struct cfe_xiocb xiocb; xiocb.xiocb_fcode = CFE_CMD_DEV_WRITE; xiocb.xiocb_status = 0; xiocb.xiocb_handle = handle; xiocb.xiocb_flags = 0; xiocb.xiocb_psize = sizeof(struct xiocb_buffer); xiocb.plist.xiocb_buffer.buf_offset = offset; xiocb.plist.xiocb_buffer.buf_ptr = XPTR_FROM_NATIVE(buffer); xiocb.plist.xiocb_buffer.buf_length = length; cfe_iocb_dispatch(&xiocb); if (xiocb.xiocb_status < 0) return xiocb.xiocb_status; return xiocb.plist.xiocb_buffer.buf_retlen; } void __init cfe_die(char *fmt, ...) { unsigned int prid, __maybe_unused rev; char msg[128]; va_list ap; int handle; unsigned int count; va_start(ap, fmt); vsprintf(msg, fmt, ap); strcat(msg, "\r\n"); if (cfe_seal != CFE_EPTSEAL) goto no_cfe; prid = read_c0_prid(); if ((prid & PRID_COMP_MASK) != PRID_COMP_BROADCOM) goto no_cfe; rev = prid & PRID_REV_MASK; /* disable XKS01 so that CFE can access the registers */ switch (prid & PRID_IMP_MASK) { #ifdef CONFIG_CPU_BMIPS4380 case PRID_IMP_BMIPS43XX: if (rev >= PRID_REV_BMIPS4380_LO && rev <= PRID_REV_BMIPS4380_HI) __write_32bit_c0_register($22, 3, __read_32bit_c0_register($22, 3) & ~BIT(12)); break; #endif #ifdef CONFIG_CPU_BMIPS5000 case PRID_IMP_BMIPS5000: case PRID_IMP_BMIPS5200: __write_32bit_c0_register($22, 5, __read_32bit_c0_register($22, 5) & ~BIT(8)); break; #endif default: break; } handle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE); if (handle < 0) goto no_cfe; cfe_write(handle, msg, strlen(msg)); for (count = 0; count < 0x7fffffff; count++) mb(); cfe_exit(0, 1); while (1) ; no_cfe: /* probably won't print anywhere useful */ panic("%s", msg); va_end(ap); }
linux-master
arch/mips/fw/cfe/cfe_api.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2000, 2001 Broadcom Corporation * * Copyright (C) 2002 MontaVista Software Inc. * Author: [email protected] or [email protected] */ #include <linux/bcd.h> #include <linux/types.h> #include <linux/time.h> #include <asm/time.h> #include <asm/addrspace.h> #include <asm/io.h> #include <asm/sibyte/sb1250.h> #include <asm/sibyte/sb1250_regs.h> #include <asm/sibyte/sb1250_smbus.h> /* Xicor 1241 definitions */ /* * Register bits */ #define X1241REG_SR_BAT 0x80 /* currently on battery power */ #define X1241REG_SR_RWEL 0x04 /* r/w latch is enabled, can write RTC */ #define X1241REG_SR_WEL 0x02 /* r/w latch is unlocked, can enable r/w now */ #define X1241REG_SR_RTCF 0x01 /* clock failed */ #define X1241REG_BL_BP2 0x80 /* block protect 2 */ #define X1241REG_BL_BP1 0x40 /* block protect 1 */ #define X1241REG_BL_BP0 0x20 /* block protect 0 */ #define X1241REG_BL_WD1 0x10 #define X1241REG_BL_WD0 0x08 #define X1241REG_HR_MIL 0x80 /* military time format */ /* * Register numbers */ #define X1241REG_BL 0x10 /* block protect bits */ #define X1241REG_INT 0x11 /* */ #define X1241REG_SC 0x30 /* Seconds */ #define X1241REG_MN 0x31 /* Minutes */ #define X1241REG_HR 0x32 /* Hours */ #define X1241REG_DT 0x33 /* Day of month */ #define X1241REG_MO 0x34 /* Month */ #define X1241REG_YR 0x35 /* Year */ #define X1241REG_DW 0x36 /* Day of Week */ #define X1241REG_Y2K 0x37 /* Year 2K */ #define X1241REG_SR 0x3F /* Status register */ #define X1241_CCR_ADDRESS 0x6F #define SMB_CSR(reg) IOADDR(A_SMB_REGISTER(1, reg)) static int xicor_read(uint8_t addr) { while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY) ; __raw_writeq((addr >> 8) & 0x7, SMB_CSR(R_SMB_CMD)); __raw_writeq(addr & 0xff, SMB_CSR(R_SMB_DATA)); __raw_writeq(V_SMB_ADDR(X1241_CCR_ADDRESS) | V_SMB_TT_WR2BYTE, SMB_CSR(R_SMB_START)); while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY) ; __raw_writeq(V_SMB_ADDR(X1241_CCR_ADDRESS) | V_SMB_TT_RD1BYTE, SMB_CSR(R_SMB_START)); while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY) ; if (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_ERROR) { /* Clear error bit by writing a 1 */ __raw_writeq(M_SMB_ERROR, SMB_CSR(R_SMB_STATUS)); return -1; } return __raw_readq(SMB_CSR(R_SMB_DATA)) & 0xff; } static int xicor_write(uint8_t addr, int b) { while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY) ; __raw_writeq(addr, SMB_CSR(R_SMB_CMD)); __raw_writeq((addr & 0xff) | ((b & 0xff) << 8), SMB_CSR(R_SMB_DATA)); __raw_writeq(V_SMB_ADDR(X1241_CCR_ADDRESS) | V_SMB_TT_WR3BYTE, SMB_CSR(R_SMB_START)); while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY) ; if (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_ERROR) { /* Clear error bit by writing a 1 */ __raw_writeq(M_SMB_ERROR, SMB_CSR(R_SMB_STATUS)); return -1; } else { return 0; } } int xicor_set_time(time64_t t) { struct rtc_time tm; int tmp; unsigned long flags; rtc_time64_to_tm(t, &tm); tm.tm_year += 1900; spin_lock_irqsave(&rtc_lock, flags); /* unlock writes to the CCR */ xicor_write(X1241REG_SR, X1241REG_SR_WEL); xicor_write(X1241REG_SR, X1241REG_SR_WEL | X1241REG_SR_RWEL); /* trivial ones */ tm.tm_sec = bin2bcd(tm.tm_sec); xicor_write(X1241REG_SC, tm.tm_sec); tm.tm_min = bin2bcd(tm.tm_min); xicor_write(X1241REG_MN, tm.tm_min); tm.tm_mday = bin2bcd(tm.tm_mday); xicor_write(X1241REG_DT, tm.tm_mday); /* tm_mon starts from 0, *ick* */ tm.tm_mon ++; tm.tm_mon = bin2bcd(tm.tm_mon); xicor_write(X1241REG_MO, tm.tm_mon); /* year is split */ tmp = tm.tm_year / 100; tm.tm_year %= 100; xicor_write(X1241REG_YR, tm.tm_year); xicor_write(X1241REG_Y2K, tmp); /* hour is the most tricky one */ tmp = xicor_read(X1241REG_HR); if (tmp & X1241REG_HR_MIL) { /* 24 hour format */ tm.tm_hour = bin2bcd(tm.tm_hour); tmp = (tmp & ~0x3f) | (tm.tm_hour & 0x3f); } else { /* 12 hour format, with 0x2 for pm */ tmp = tmp & ~0x3f; if (tm.tm_hour >= 12) { tmp |= 0x20; tm.tm_hour -= 12; } tm.tm_hour = bin2bcd(tm.tm_hour); tmp |= tm.tm_hour; } xicor_write(X1241REG_HR, tmp); xicor_write(X1241REG_SR, 0); spin_unlock_irqrestore(&rtc_lock, flags); return 0; } time64_t xicor_get_time(void) { unsigned int year, mon, day, hour, min, sec, y2k; unsigned long flags; spin_lock_irqsave(&rtc_lock, flags); sec = xicor_read(X1241REG_SC); min = xicor_read(X1241REG_MN); hour = xicor_read(X1241REG_HR); if (hour & X1241REG_HR_MIL) { hour &= 0x3f; } else { if (hour & 0x20) hour = (hour & 0xf) + 0x12; } day = xicor_read(X1241REG_DT); mon = xicor_read(X1241REG_MO); year = xicor_read(X1241REG_YR); y2k = xicor_read(X1241REG_Y2K); spin_unlock_irqrestore(&rtc_lock, flags); sec = bcd2bin(sec); min = bcd2bin(min); hour = bcd2bin(hour); day = bcd2bin(day); mon = bcd2bin(mon); year = bcd2bin(year); y2k = bcd2bin(y2k); year += (y2k * 100); return mktime64(year, mon, day, hour, min, sec); } int xicor_probe(void) { return xicor_read(X1241REG_SC) != -1; }
linux-master
arch/mips/sibyte/swarm/rtc_xicor1241.c
// SPDX-License-Identifier: GPL-2.0 #include <linux/err.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/io.h> #include <linux/platform_device.h> #include <linux/ata_platform.h> #include <asm/sibyte/board.h> #include <asm/sibyte/sb1250_genbus.h> #include <asm/sibyte/sb1250_regs.h> #if defined(CONFIG_SIBYTE_SWARM) || defined(CONFIG_SIBYTE_LITTLESUR) #define DRV_NAME "pata-swarm" #define SWARM_IDE_SHIFT 5 #define SWARM_IDE_BASE 0x1f0 #define SWARM_IDE_CTRL 0x3f6 static struct resource swarm_pata_resource[] = { { .name = "Swarm GenBus IDE", .flags = IORESOURCE_MEM, }, { .name = "Swarm GenBus IDE", .flags = IORESOURCE_MEM, }, { .name = "Swarm GenBus IDE", .flags = IORESOURCE_IRQ, .start = K_INT_GB_IDE, .end = K_INT_GB_IDE, }, }; static struct pata_platform_info pata_platform_data = { .ioport_shift = SWARM_IDE_SHIFT, }; static struct platform_device swarm_pata_device = { .name = "pata_platform", .id = -1, .resource = swarm_pata_resource, .num_resources = ARRAY_SIZE(swarm_pata_resource), .dev = { .platform_data = &pata_platform_data, .coherent_dma_mask = ~0, /* grumble */ }, }; static int __init swarm_pata_init(void) { u8 __iomem *base; phys_addr_t offset, size; struct resource *r; if (!SIBYTE_HAVE_IDE) return -ENODEV; base = ioremap(A_IO_EXT_BASE, 0x800); offset = __raw_readq(base + R_IO_EXT_REG(R_IO_EXT_START_ADDR, IDE_CS)); size = __raw_readq(base + R_IO_EXT_REG(R_IO_EXT_MULT_SIZE, IDE_CS)); iounmap(base); offset = G_IO_START_ADDR(offset) << S_IO_ADDRBASE; size = (G_IO_MULT_SIZE(size) + 1) << S_IO_REGSIZE; if (offset < A_PHYS_GENBUS || offset >= A_PHYS_GENBUS_END) { pr_info(DRV_NAME ": PATA interface at GenBus disabled\n"); return -EBUSY; } pr_info(DRV_NAME ": PATA interface at GenBus slot %i\n", IDE_CS); r = swarm_pata_resource; r[0].start = offset + (SWARM_IDE_BASE << SWARM_IDE_SHIFT); r[0].end = offset + ((SWARM_IDE_BASE + 8) << SWARM_IDE_SHIFT) - 1; r[1].start = offset + (SWARM_IDE_CTRL << SWARM_IDE_SHIFT); r[1].end = offset + ((SWARM_IDE_CTRL + 1) << SWARM_IDE_SHIFT) - 1; return platform_device_register(&swarm_pata_device); } device_initcall(swarm_pata_init); #endif /* defined(CONFIG_SIBYTE_SWARM) || defined(CONFIG_SIBYTE_LITTLESUR) */ #define sb1250_dev_struct(num) \ static struct resource sb1250_res##num = { \ .name = "SB1250 MAC " __stringify(num), \ .flags = IORESOURCE_MEM, \ .start = A_MAC_CHANNEL_BASE(num), \ .end = A_MAC_CHANNEL_BASE(num + 1) - 1, \ };\ static struct platform_device sb1250_dev##num = { \ .name = "sb1250-mac", \ .id = num, \ .resource = &sb1250_res##num, \ .num_resources = 1, \ } sb1250_dev_struct(0); sb1250_dev_struct(1); sb1250_dev_struct(2); sb1250_dev_struct(3); static struct platform_device *sb1250_devs[] __initdata = { &sb1250_dev0, &sb1250_dev1, &sb1250_dev2, &sb1250_dev3, }; static int __init sb1250_device_init(void) { int ret; /* Set the number of available units based on the SOC type. */ switch (soc_type) { case K_SYS_SOC_TYPE_BCM1250: case K_SYS_SOC_TYPE_BCM1250_ALT: ret = platform_add_devices(sb1250_devs, 3); break; case K_SYS_SOC_TYPE_BCM1120: case K_SYS_SOC_TYPE_BCM1125: case K_SYS_SOC_TYPE_BCM1125H: case K_SYS_SOC_TYPE_BCM1250_ALT2: /* Hybrid */ ret = platform_add_devices(sb1250_devs, 2); break; case K_SYS_SOC_TYPE_BCM1x55: case K_SYS_SOC_TYPE_BCM1x80: ret = platform_add_devices(sb1250_devs, 4); break; default: ret = -ENODEV; break; } return ret; } device_initcall(sb1250_device_init);
linux-master
arch/mips/sibyte/swarm/platform.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Broadcom BCM91250A (SWARM), etc. I2C platform setup. * * Copyright (c) 2008 Maciej W. Rozycki */ #include <linux/i2c.h> #include <linux/init.h> #include <linux/kernel.h> static struct i2c_board_info swarm_i2c_info1[] __initdata = { { I2C_BOARD_INFO("m41t81", 0x68), }, }; static int __init swarm_i2c_init(void) { int err; err = i2c_register_board_info(1, swarm_i2c_info1, ARRAY_SIZE(swarm_i2c_info1)); if (err < 0) printk(KERN_ERR "swarm-i2c: cannot register board I2C devices\n"); return err; } arch_initcall(swarm_i2c_init);
linux-master
arch/mips/sibyte/swarm/swarm-i2c.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2000, 2001, 2002, 2003, 2004 Broadcom Corporation * Copyright (C) 2004 by Ralf Baechle ([email protected]) */ /* * Setup code for the SWARM board */ #include <linux/spinlock.h> #include <linux/mm.h> #include <linux/memblock.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/screen_info.h> #include <linux/initrd.h> #include <asm/irq.h> #include <asm/io.h> #include <asm/bootinfo.h> #include <asm/mipsregs.h> #include <asm/reboot.h> #include <asm/time.h> #include <asm/traps.h> #include <asm/sibyte/sb1250.h> #ifdef CONFIG_SIBYTE_BCM1x80 #include <asm/sibyte/bcm1480_regs.h> #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X) #include <asm/sibyte/sb1250_regs.h> #else #error invalid SiByte board configuration #endif #include <asm/sibyte/sb1250_genbus.h> #include <asm/sibyte/board.h> #ifdef CONFIG_SIBYTE_BCM1x80 extern void bcm1480_setup(void); #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X) extern void sb1250_setup(void); #else #error invalid SiByte board configuration #endif extern int xicor_probe(void); extern int xicor_set_time(time64_t); extern time64_t xicor_get_time(void); extern int m41t81_probe(void); extern int m41t81_set_time(time64_t); extern time64_t m41t81_get_time(void); const char *get_system_type(void) { return "SiByte " SIBYTE_BOARD_NAME; } int swarm_be_handler(struct pt_regs *regs, int is_fixup) { if (!is_fixup && (regs->cp0_cause & 4)) { /* Data bus error - print PA */ printk("DBE physical address: %010Lx\n", __read_64bit_c0_register($26, 1)); } return is_fixup ? MIPS_BE_FIXUP : MIPS_BE_FATAL; } enum swarm_rtc_type { RTC_NONE, RTC_XICOR, RTC_M41T81, }; enum swarm_rtc_type swarm_rtc_type; void read_persistent_clock64(struct timespec64 *ts) { time64_t sec; switch (swarm_rtc_type) { case RTC_XICOR: sec = xicor_get_time(); break; case RTC_M41T81: sec = m41t81_get_time(); break; case RTC_NONE: default: sec = mktime64(2000, 1, 1, 0, 0, 0); break; } ts->tv_sec = sec; ts->tv_nsec = 0; } int update_persistent_clock64(struct timespec64 now) { time64_t sec = now.tv_sec; switch (swarm_rtc_type) { case RTC_XICOR: return xicor_set_time(sec); case RTC_M41T81: return m41t81_set_time(sec); case RTC_NONE: default: return -1; } } void __init plat_mem_setup(void) { #ifdef CONFIG_SIBYTE_BCM1x80 bcm1480_setup(); #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X) sb1250_setup(); #else #error invalid SiByte board configuration #endif mips_set_be_handler(swarm_be_handler); if (xicor_probe()) swarm_rtc_type = RTC_XICOR; if (m41t81_probe()) swarm_rtc_type = RTC_M41T81; #ifdef CONFIG_VT screen_info = (struct screen_info) { .orig_video_page = 52, .orig_video_mode = 3, .orig_video_cols = 80, .flags = 12, .orig_video_ega_bx = 3, .orig_video_lines = 25, .orig_video_isVGA = 0x22, .orig_video_points = 16, }; /* XXXKW for CFE, get lines/cols from environment */ #endif } #ifdef LEDS_PHYS void setleds(char *str) { void *reg; int i; for (i = 0; i < 4; i++) { reg = IOADDR(LEDS_PHYS) + 0x20 + ((3 - i) << 3); if (!str[i]) writeb(' ', reg); else writeb(str[i], reg); } } #endif /* LEDS_PHYS */
linux-master
arch/mips/sibyte/swarm/setup.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2000, 2001 Broadcom Corporation * * Copyright (C) 2002 MontaVista Software Inc. * Author: [email protected] or [email protected] */ #include <linux/bcd.h> #include <linux/types.h> #include <linux/time.h> #include <asm/time.h> #include <asm/addrspace.h> #include <asm/io.h> #include <asm/sibyte/sb1250.h> #include <asm/sibyte/sb1250_regs.h> #include <asm/sibyte/sb1250_smbus.h> /* M41T81 definitions */ /* * Register bits */ #define M41T81REG_SC_ST 0x80 /* stop bit */ #define M41T81REG_HR_CB 0x40 /* century bit */ #define M41T81REG_HR_CEB 0x80 /* century enable bit */ #define M41T81REG_CTL_S 0x20 /* sign bit */ #define M41T81REG_CTL_FT 0x40 /* frequency test bit */ #define M41T81REG_CTL_OUT 0x80 /* output level */ #define M41T81REG_WD_RB0 0x01 /* watchdog resolution bit 0 */ #define M41T81REG_WD_RB1 0x02 /* watchdog resolution bit 1 */ #define M41T81REG_WD_BMB0 0x04 /* watchdog multiplier bit 0 */ #define M41T81REG_WD_BMB1 0x08 /* watchdog multiplier bit 1 */ #define M41T81REG_WD_BMB2 0x10 /* watchdog multiplier bit 2 */ #define M41T81REG_WD_BMB3 0x20 /* watchdog multiplier bit 3 */ #define M41T81REG_WD_BMB4 0x40 /* watchdog multiplier bit 4 */ #define M41T81REG_AMO_ABE 0x20 /* alarm in "battery back-up mode" enable bit */ #define M41T81REG_AMO_SQWE 0x40 /* square wave enable */ #define M41T81REG_AMO_AFE 0x80 /* alarm flag enable flag */ #define M41T81REG_ADT_RPT5 0x40 /* alarm repeat mode bit 5 */ #define M41T81REG_ADT_RPT4 0x80 /* alarm repeat mode bit 4 */ #define M41T81REG_AHR_RPT3 0x80 /* alarm repeat mode bit 3 */ #define M41T81REG_AHR_HT 0x40 /* halt update bit */ #define M41T81REG_AMN_RPT2 0x80 /* alarm repeat mode bit 2 */ #define M41T81REG_ASC_RPT1 0x80 /* alarm repeat mode bit 1 */ #define M41T81REG_FLG_AF 0x40 /* alarm flag (read only) */ #define M41T81REG_FLG_WDF 0x80 /* watchdog flag (read only) */ #define M41T81REG_SQW_RS0 0x10 /* sqw frequency bit 0 */ #define M41T81REG_SQW_RS1 0x20 /* sqw frequency bit 1 */ #define M41T81REG_SQW_RS2 0x40 /* sqw frequency bit 2 */ #define M41T81REG_SQW_RS3 0x80 /* sqw frequency bit 3 */ /* * Register numbers */ #define M41T81REG_TSC 0x00 /* tenths/hundredths of second */ #define M41T81REG_SC 0x01 /* seconds */ #define M41T81REG_MN 0x02 /* minute */ #define M41T81REG_HR 0x03 /* hour/century */ #define M41T81REG_DY 0x04 /* day of week */ #define M41T81REG_DT 0x05 /* date of month */ #define M41T81REG_MO 0x06 /* month */ #define M41T81REG_YR 0x07 /* year */ #define M41T81REG_CTL 0x08 /* control */ #define M41T81REG_WD 0x09 /* watchdog */ #define M41T81REG_AMO 0x0A /* alarm: month */ #define M41T81REG_ADT 0x0B /* alarm: date */ #define M41T81REG_AHR 0x0C /* alarm: hour */ #define M41T81REG_AMN 0x0D /* alarm: minute */ #define M41T81REG_ASC 0x0E /* alarm: second */ #define M41T81REG_FLG 0x0F /* flags */ #define M41T81REG_SQW 0x13 /* square wave register */ #define M41T81_CCR_ADDRESS 0x68 #define SMB_CSR(reg) IOADDR(A_SMB_REGISTER(1, reg)) static int m41t81_read(uint8_t addr) { while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY) ; __raw_writeq(addr & 0xff, SMB_CSR(R_SMB_CMD)); __raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_WR1BYTE, SMB_CSR(R_SMB_START)); while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY) ; __raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_RD1BYTE, SMB_CSR(R_SMB_START)); while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY) ; if (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_ERROR) { /* Clear error bit by writing a 1 */ __raw_writeq(M_SMB_ERROR, SMB_CSR(R_SMB_STATUS)); return -1; } return __raw_readq(SMB_CSR(R_SMB_DATA)) & 0xff; } static int m41t81_write(uint8_t addr, int b) { while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY) ; __raw_writeq(addr & 0xff, SMB_CSR(R_SMB_CMD)); __raw_writeq(b & 0xff, SMB_CSR(R_SMB_DATA)); __raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_WR2BYTE, SMB_CSR(R_SMB_START)); while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY) ; if (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_ERROR) { /* Clear error bit by writing a 1 */ __raw_writeq(M_SMB_ERROR, SMB_CSR(R_SMB_STATUS)); return -1; } /* read the same byte again to make sure it is written */ __raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_RD1BYTE, SMB_CSR(R_SMB_START)); while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY) ; return 0; } int m41t81_set_time(time64_t t) { struct rtc_time tm; unsigned long flags; /* Note we don't care about the century */ rtc_time64_to_tm(t, &tm); /* * Note the write order matters as it ensures the correctness. * When we write sec, 10th sec is clear. It is reasonable to * believe we should finish writing min within a second. */ spin_lock_irqsave(&rtc_lock, flags); tm.tm_sec = bin2bcd(tm.tm_sec); m41t81_write(M41T81REG_SC, tm.tm_sec); tm.tm_min = bin2bcd(tm.tm_min); m41t81_write(M41T81REG_MN, tm.tm_min); tm.tm_hour = bin2bcd(tm.tm_hour); tm.tm_hour = (tm.tm_hour & 0x3f) | (m41t81_read(M41T81REG_HR) & 0xc0); m41t81_write(M41T81REG_HR, tm.tm_hour); /* tm_wday starts from 0 to 6 */ if (tm.tm_wday == 0) tm.tm_wday = 7; tm.tm_wday = bin2bcd(tm.tm_wday); m41t81_write(M41T81REG_DY, tm.tm_wday); tm.tm_mday = bin2bcd(tm.tm_mday); m41t81_write(M41T81REG_DT, tm.tm_mday); /* tm_mon starts from 0, *ick* */ tm.tm_mon ++; tm.tm_mon = bin2bcd(tm.tm_mon); m41t81_write(M41T81REG_MO, tm.tm_mon); /* we don't do century, everything is beyond 2000 */ tm.tm_year %= 100; tm.tm_year = bin2bcd(tm.tm_year); m41t81_write(M41T81REG_YR, tm.tm_year); spin_unlock_irqrestore(&rtc_lock, flags); return 0; } time64_t m41t81_get_time(void) { unsigned int year, mon, day, hour, min, sec; unsigned long flags; /* * min is valid if two reads of sec are the same. */ for (;;) { spin_lock_irqsave(&rtc_lock, flags); sec = m41t81_read(M41T81REG_SC); min = m41t81_read(M41T81REG_MN); if (sec == m41t81_read(M41T81REG_SC)) break; spin_unlock_irqrestore(&rtc_lock, flags); } hour = m41t81_read(M41T81REG_HR) & 0x3f; day = m41t81_read(M41T81REG_DT); mon = m41t81_read(M41T81REG_MO); year = m41t81_read(M41T81REG_YR); spin_unlock_irqrestore(&rtc_lock, flags); sec = bcd2bin(sec); min = bcd2bin(min); hour = bcd2bin(hour); day = bcd2bin(day); mon = bcd2bin(mon); year = bcd2bin(year); year += 2000; return mktime64(year, mon, day, hour, min, sec); } int m41t81_probe(void) { unsigned int tmp; /* enable chip if it is not enabled yet */ tmp = m41t81_read(M41T81REG_SC); m41t81_write(M41T81REG_SC, tmp & 0x7f); return m41t81_read(M41T81REG_SC) != -1; }
linux-master
arch/mips/sibyte/swarm/rtc_m41t81.c